# coding=utf8

import vim
import re
import os
import subprocess
import glob

import util

PHP_SYNTAX_ITEMS = [
    'String',
    'Keyword',
    'Conditional',
    'Number',
    'Type',
]

def get_class_name_with_underscored_namespaces(path=""):
    """ Returns a class name with expanded namespaces from path variable """
    if not path:
        path = vim.eval('expand("%p")')

    path = path.replace('.php', '')
    class_name = re.sub(r'(?!^)\/([a-zA-Z])', lambda m: '_' + m.group(1).upper(), path)
    return class_name

def cycle_by_var_name(identifiers=[]):
    line_number, column_number = vim.current.window.cursor

    identifier = get_identifier_under_cursor()
    new_identifier_data = get_last_used_var(identifier, identifiers)

    if not new_identifier_data:
        return

    (new_identifier, new_identifier_line, new_identifier_column) = \
        new_identifier_data

    buffer = vim.current.window.buffer
    line = buffer[line_number-1]

    if column_number-len(identifier) >= 0:
        buffer[line_number-1] = line[:column_number-len(identifier)]
    else:
        buffer[line_number-1] = ""

    buffer[line_number-1] += new_identifier + line[column_number:]

    vim.current.window.cursor = (
        line_number,
        column_number-len(identifier)+len(new_identifier)
    )

    util.highlight(
        new_identifier_line,
        new_identifier_column,
        len(new_identifier)
    )

    return new_identifier

def get_all_last_used_identifiers():
    search_space = util.get_buffer_before_cursor()
    line_number, _ = vim.current.window.cursor
    identifiers = []

    for line in reversed(search_space.split('\n')):
        line_number -= 1
        matches = re.finditer(r'(protected|public|private|var)?\s?(\$[\w_\->]+)(?![\w]*\()', line)

        if not matches:
            continue

        for match in reversed(list(matches)):
            is_class_member = match.groups(0)[0]
            identifier = match.groups(0)[1]

            identifiers.append((identifier, line_number, match.start(2)+1))

            if is_class_member:
                identifiers.append(('$this->' + identifier[1:], line_number,
                    match.start(2)+1))

    return identifiers

def get_identifier_from_string(string, with_dollar=True):
    matches = re.search('(\$[\w_\->]+)', string)

    if not matches:
        return ""

    result = matches.group(1)

    if with_dollar == False and result[0] == '$':
        result = result[1:]

    return result

def get_identifier_under_cursor(with_dollar=True):
    line_number, column_number = vim.current.window.cursor
    line = vim.current.window.buffer[line_number-1][:column_number]

    return get_identifier_from_string(line, with_dollar)

def smart_convert_to_camelcase(identifier):
    """
    Smart converting variables to camelCase names.
    """
    if identifier[0] == '_':
        identifier = identifier[1:]

    identifier = identifier.title()

    return identifier

def get_camelcase_identifier_from_string(string, with_dollar=True):
    """
    Returns camelCase identifier from string, for example:
    `protected $_storage;`
    returns just `Storage`
    """
    return smart_convert_to_camelcase(get_identifier_from_string(string, with_dollar))

def get_last_used_var(prev_var='', identifiers=[]):
    if not identifiers:
        identifiers = get_all_last_used_identifiers()

    if prev_var:
        prev_var_match_found = False
    else:
        prev_var_match_found = True

    walked = {'$this': True}
    for identifier_data in identifiers:
        (identifier, line, column) = identifier_data

        syntax_name = util.get_syntax_name(line, column)

        if (syntax_name in PHP_SYNTAX_ITEMS):
            continue

        if identifier in walked:
            continue

        if prev_var and identifier.startswith(prev_var):
            if prev_var != identifier:
                return identifier_data

        if prev_var_match_found:
            return identifier_data

        walked[identifier] = True

        if (prev_var == identifier):
            prev_var_match_found = True

def get_phpdoc_variables_before_cursor():
    """
    Returns key-value data from phpdoc before current cursor position.
    Sometimes value may be array
    """
    line_number, _ = vim.current.window.cursor
    buffer = vim.current.window.buffer

    variables = {}
    i = 0
    while i < 10:
        i = i + 1
        line = buffer[line_number-i]
        matches = re.search('\* \@([\w]+) (.*)', line)
        if not matches:
            continue

        name = matches.group(1)
        value = matches.group(2)

        if name in variables:
            if isinstance(variables[name], list):
                variables[name].insert(0, value)
            else:
                variables[name] = [variables[name], value]
        else:
            variables[name] = value

    return variables


import unittest
from tests.fakes.repositories.fake_search_repository import FakeSearchRepository
from sf_real_estate.create_search.create_search_use_case import CreateSearchUseCase
from sf_real_estate.values.search import Search
from tests.assertions import assert_searches_equal

class FakeCreateObserver(object):
    def __init__(self):
        self.created_search = None
        self.created_identifier = None

    def did_create_search(self, identifier, search):
        self.created_search = search
        self.created_identifier = identifier

    def did_fail_to_present_search_identifier(self, exception, context):
        self.exception = exception
        self.context = context

class CreateSearchUseCaseTests(unittest.TestCase):
    def setUp(self):
        super(CreateSearchUseCaseTests, self).setUp()
        self.repository = FakeSearchRepository()

        self.subject = CreateSearchUseCase(search_repository=self.repository)

    def test_create_search_with_valid_county_and_address_notifies_observer(self):
        observer = FakeCreateObserver()
        self.subject.execute(address='125 Jones St', county='sanfrancisco', observer=observer)

        search = None
        def fetch_success(incoming_search):
            nonlocal search
            search = incoming_search

        fetch_failure = lambda x, y: None
        self.repository.fetch_search(identifier=observer.created_identifier,
                                     success=fetch_success,
                                     failure=fetch_failure)

        expected_fetched_search = Search(county='sanfrancisco',
                                         address='125 Jones St')

        assert_searches_equal(observer.created_search, expected_fetched_search, self)


from django.conf.urls import patterns, url

from docorder import views

urlpatterns = patterns('',
                       #ex: /docorder/
                       url(r'^$', views.load, name='load'),
                       #ex: /docorder/compare
                       url(r'^compare/$', views.compare, name='compare'),
                       #ex: /docorder/select
                       url(r'^select/$', views.select, name='select'),
                       #ex: /docorder/order
                       url(r'^order/$', views.order, name='order'),
                       url(r'^selectExp/(?P<exp>.*)/$', views.selectExp, name='selectExp'),
)


import unittest

from teamcity.unittestpy import TeamcityTestRunner

class TestXXX(unittest.TestCase):
    def testSubtestSuccess(self):
        for i in range(0, 2):
            with self.subTest(i=i):
                self.assertEqual(i, i)

unittest.main(testRunner=TeamcityTestRunner)


import os
import time

import numpy as np
import scipy.misc
import tensorflow as tf

from RNNSeg.LSTM_Network import BiLSTMNetwork
from RNNSeg.Params import ParamsEvalBiLSTM

def run_net():

    # Data input
    data_provider = params.data_provider

    with tf.name_scope('Data'):
        image_seq_fw, filename_seq_fw, image_seq_bw, filename_seq_bw = data_provider.get_sequence(params.seq_length)
    image_seq_fw_bw = [tf.concat([fw, bw], axis=0) for fw, bw in zip(image_seq_fw, image_seq_bw[::-1])]
    filename_seq_bw = filename_seq_bw[::-1]
    # Build Network Graph
    net_fw = BiLSTMNetwork()
    net_bw = BiLSTMNetwork()

    with tf.device('/gpu:0'):
        with tf.name_scope('run_tower'):
            image_seq_norm_fw = [tf.div(tf.subtract(im, params.norm),
                                        params.norm) for im in image_seq_fw]
            image_seq_norm_bw = [tf.div(tf.subtract(im, params.norm),
                                        params.norm) for im in image_seq_bw[::-1]]
            with tf.variable_scope('net'):
                _ = net_fw.build(image_seq_norm_fw, phase_train=True, net_params=params.net_params, max_len=4)
            with tf.variable_scope('net', reuse=True):
                _ = net_bw.build(image_seq_norm_bw, phase_train=True, net_params=params.net_params, max_len=4)
            fw_outputs = net_fw.fw_outputs
            bw_outputs = net_bw.bw_outputs

        fw_ph = tf.placeholder(tf.float32, params.image_size + (3,), 'fw_placeholder')
        bw_ph = tf.placeholder(tf.float32, params.image_size + (3,), 'bw_placeholder')
        merged = tf.add(fw_ph, bw_ph)
        final_out = tf.nn.softmax(merged)

    saver = tf.train.Saver(var_list=tf.global_variables())
    init_op = tf.group(tf.local_variables_initializer())

    config = tf.ConfigProto(log_device_placement=False, allow_soft_placement=True)
    config.gpu_options.allow_growth = True

    coord = tf.train.Coordinator()
    with tf.Session(config=config) as sess:
        sess.run(init_op)
        if params.load_checkpoint:
            saver.restore(sess, params.load_checkpoint_path)

        threads = tf.train.start_queue_runners(sess, coord=coord)
        elapsed_time = 0.
        end_time = 0.
        other_time = 0.
        options = tf.RunOptions()
        feed_dict = {}
        loop = True
        all_filenames = []
        sigout =  [tf.nn.softmax(tf.transpose(o,(0,2,3,1))) for o in net_fw.outputs]
        t = 0
        while loop:
            try:
                t+=1
                start_time = time.time()
                other_time += start_time - end_time
                fetch_out = sess.run([fw_outputs, bw_outputs, net_fw.states[-1], net_bw.states_back[-1], filename_seq_fw, filename_seq_bw, sigout, image_seq_fw],
                                     options=options, feed_dict=feed_dict)
                seg_seq_out_fw, seg_seq_out_bw, states_fw, states_bw, file_names_fw, file_names_bw, sigoutnp, imin = fetch_out
                end_time = time.time()
                elapsed_time += end_time-start_time

                for state_ph, last_state in zip(net_fw.states[0], states_fw):
                    feed_dict[state_ph[0]] = last_state[0]*4/np.float32(t)
                    feed_dict[state_ph[1]] = last_state[1]
                    print(last_state[0].max(), last_state[0].min())
                    print(last_state[1].max(), last_state[1].min())

                for state_ph, last_state in zip(net_bw.states_back[0], states_bw):
                    feed_dict[state_ph[0]] = last_state[0]*4/np.float32(t)
                    feed_dict[state_ph[1]] = last_state[1]

                if not params.dry_run:
                    out_dir = params.experiment_tmp_fw_dir
                    for file_name, image_seg in zip(file_names_fw, seg_seq_out_fw):
                        file_name = file_name.decode('utf-8')
                        fw_squeeze = np.squeeze(image_seg)
                        fw_squeeze = fw_squeeze.transpose([1, 2, 0])
                        fw_filename = os.path.join(out_dir, os.path.basename(file_name))
                        np.save(fw_filename,fw_squeeze)
                        all_filenames.append(os.path.basename(file_name)+'.npy')
                        print("Saved File: {}".format(os.path.join(out_dir, os.path.basename(file_name))))
                    out_dir = params.experiment_tmp_bw_dir
                    for file_name, image_seg in zip(file_names_bw, seg_seq_out_bw):
                        file_name = file_name.decode('utf-8')
                        bw_squeeze = np.squeeze(image_seg)
                        bw_squeeze = bw_squeeze.transpose([1, 2, 0])
                        bw_filename = os.path.join(out_dir, os.path.basename(file_name))
                        np.save(bw_filename, bw_squeeze)
                        print("Saved File: {}".format(os.path.join(out_dir, os.path.basename(file_name))))

            except (ValueError, RuntimeError, KeyboardInterrupt, tf.errors.OutOfRangeError):

                coord.request_stop()
                coord.join(threads)
                loop = False
        if not params.dry_run:
            out_dir = params.experiment_out_dir
            for file_name in all_filenames:
                fw_logits = np.load(os.path.join(params.experiment_tmp_fw_dir, file_name))
                bw_logits = np.load(os.path.join(params.experiment_tmp_bw_dir, file_name))
                feed_dict = {bw_ph: bw_logits, fw_ph: fw_logits}
                seg_out = sess.run(final_out, feed_dict)
                seg_out = (seg_out*255).astype(np.uint8)
                scipy.misc.toimage(seg_out, cmin=0.0,
                                   cmax=255.).save(os.path.join(out_dir, os.path.basename(file_name[:-4])))
                print("Saved File: {}".format(os.path.join(out_dir, os.path.basename(file_name[:-4]))))

        coord.request_stop()
        coord.join(threads)

if __name__ == '__main__':

    params = ParamsEvalBiLSTM()
    run_net()


def add_binary(a, b):
    if a is None or len(a) == 0:
        return b
    if b is None or len(b) == 0:
        return a
    i = len(a) - 1
    j = len(b) - 1
    carry = 0
    result = ""
    while i >= 0 or j >= 0:
        sum_ = carry
        if i >= 0:
            sum_ += ord(a[i]) - ord('0')
            i -= 1
        if j >= 0:
            sum_ += ord(b[j]) - ord('0')
            j -= 1
        result = str(sum_ % 2) + result
        carry = sum_ // 2
    if carry > 0:
        result = str(carry) + result
    return result

if __name__ == '__main__':
    a_ = "1010"
    b_ = "1011"
    print(add_binary(a_, b_))


from urllib.parse import urlparse

class URLparse:
    def __init__(self):
        self.host = ""  # each object's instance variables
        self.port = 0
        self.path = ""  # remote host name
        self.query = ""
        self.path = ""

    def parse(self,url):

        o = urlparse(url)
        if(o.netloc) != '':
            self.host = o.netloc

        if(o.port != None):
            self.port = o.port
        else:
            self.port = 80

        if(o.path != ''):
            self.path=o.path
        else:
            self.path = '/'

        if(o.query != ''):
            self.query=o.query
        else:
            self.query=''

        return self.host, self.path, self.query, self.port


import unittest

from cloudrail.dev_tools.rule_test_utils import create_empty_entity
from cloudrail.knowledge.context.aws.resources.autoscaling.launch_configuration import LaunchConfiguration
from cloudrail.knowledge.context.aws.resources.autoscaling.launch_template import LaunchTemplate
from cloudrail.knowledge.context.aws.resources.ec2.ec2_instance import Ec2Instance
from cloudrail.knowledge.context.aws.aws_environment_context import AwsEnvironmentContext
from cloudrail.knowledge.rules.aws.non_context_aware.protocol_enforcments.ensure_imdsv2_is_used_rule import EnsureImdsv2IsUsedRule
from cloudrail.knowledge.rules.base_rule import RuleResultType

class TestEnsureImdsv2IsUsedRule(unittest.TestCase):
    def setUp(self):
        self.rule = EnsureImdsv2IsUsedRule()

    def test_non_car_ensure_imdsv2_fail(self):
        # Arrange
        ec2: Ec2Instance = create_empty_entity(Ec2Instance)
        launch_config: LaunchConfiguration = create_empty_entity(LaunchConfiguration)
        launch_template: LaunchTemplate = create_empty_entity(LaunchTemplate)
        ec2.http_tokens = 'disabled'
        launch_config.http_tokens = 'disabled'
        launch_template.http_token = 'disabled'
        context = AwsEnvironmentContext(ec2s=[ec2], launch_configurations=[launch_config], launch_templates=[launch_template])
        # Act
        result = self.rule.run(context, {})
        # Assert
        self.assertEqual(RuleResultType.FAILED, result.status)
        self.assertEqual(3, len(result.issues))

    def test_non_car_ensure_imdsv2_pass(self):
        # Arrange
        ec2: Ec2Instance = create_empty_entity(Ec2Instance)
        launch_config: LaunchConfiguration = create_empty_entity(LaunchConfiguration)
        launch_template: LaunchTemplate = create_empty_entity(LaunchTemplate)
        ec2.http_tokens = 'required'
        launch_config.http_tokens = 'required'
        launch_template.http_token = 'required'
        context = AwsEnvironmentContext(ec2s=[ec2], launch_configurations=[launch_config], launch_templates=[launch_template])
        # Act
        result = self.rule.run(context, {})
        # Assert
        self.assertEqual(RuleResultType.SUCCESS, result.status)
        self.assertEqual(0, len(result.issues))


import json
import math
import mmap
import numpy as np
from functools import partial
from random import shuffle
from keras.utils import Sequence
from keras.preprocessing.sequence import pad_sequences

def find_lines(data):
    for i, char in enumerate(data):
        if char == b'\n':
            yield i

def get_mmapdata_and_byteoffset(feature_file):
    ########################################################################
    # Biggest saviour: shuffling a large file w/o loading in memory
    # >>> https://stackoverflow.com/questions/24492331/shuffle-a-large-list-of-items-without-loading-in-memory
    ########################################################################

    # Creating a list of byte offset for each line
    with open(feature_file, 'r') as f:
        mmap_data = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
        start = 0
        byte_offset = []
        for end in find_lines(mmap_data):
            byte_offset.append((start, end))
            start = end + 1
    return mmap_data, byte_offset

def get_min_max(mmap_data, byte_offset):
    min_feature = None
    max_feature = None
    for start,end in byte_offset:
        dataline = mmap_data[start:end+1].decode('ascii').strip().rstrip(',')
        dataline = np.array(json.loads('['+dataline+']'))
        if min_feature is not None:
            dataline = np.vstack((min_feature, dataline))
        if max_feature is not None:
            dataline = np.vstack((max_feature, dataline))
        min_feature = np.min(dataline, axis=0)
        max_feature = np.max(dataline, axis=0)
    return (min_feature, max_feature)

def split_train_test(byte_offset, split_ratio, seed):
    # Shuffling the indices to give a random train test split
    indices = np.random.RandomState(seed=seed).permutation(len(byte_offset))
    split_idx = math.ceil((1-split_ratio)*len(byte_offset))
    train_idx, test_idx = indices[:split_idx], indices[split_idx:]
    train_byte_offset = [byte_offset[idx] for idx in train_idx]
    test_byte_offset = [byte_offset[idx] for idx in test_idx]

    return train_byte_offset, test_byte_offset

def normalize(option, min_max_feature=None):
    def l2_norm(batch_data):
        l2_norm = np.linalg.norm(batch_data, axis=2, keepdims=True)
        batch_data = np.divide(batch_data, l2_norm, out=np.zeros_like(batch_data), where=l2_norm!=0.0)
        return batch_data
    def min_max_norm(batch_data, min_max_feature):
        num = batch_data-min_max_feature[0]
        den = min_max_feature[1]-min_max_feature[0]
        batch_data = np.divide(num, den, out=np.zeros_like(num), where=den!=0.0)
        return batch_data
    if option == 1:
        return l2_norm
    elif option == 2:
        if min_max_feature is not None:
            return partial(min_max_norm, min_max_feature=min_max_feature)
        else:
            print("Error: min-max range for feature is not provided")
            return

class BatchGenerator(Sequence):
    def __init__(self, mmap_data, byte_offset, batch_size, sequence_len, norm_fn, return_seq_len=False):
        self.mmap_data = mmap_data
        self.byte_offset = byte_offset
        self.batch_size = batch_size
        self.sequence_len = sequence_len
        self.norm_fn = norm_fn
        # self.min_feature = min_max_feature[0]
        # self.max_feature = min_max_feature[1]
        self.return_seq_len = return_seq_len

    def __len__(self):
        return int(np.ceil(len(self.byte_offset)/float(self.batch_size)))

    def __getitem__(self, idx):
        batch_idx = self.byte_offset[idx*self.batch_size:(idx+1)*self.batch_size]
        batch_data = []
        for start,end in batch_idx:
            dataline = self.mmap_data[start:end+1].decode('ascii').strip().rstrip(',')
            batch_data.append(json.loads('['+dataline+']'))

        if self.return_seq_len:
            batch_seq_len = [len(data) for data in batch_data]

        # Pad the sequence
        batch_data = pad_sequences(batch_data, maxlen=self.sequence_len, dtype='float32', padding='post',value=0.0)

        # Scale the features
        batch_data = self.norm_fn(batch_data)
        # (1)   L2 Normalization
        # l2_norm = np.linalg.norm(batch_data, axis=2, keepdims=True)
        # batch_data = np.divide(batch_data, l2_norm, out=np.zeros_like(batch_data), where=l2_norm!=0.0)
        # (2)   Min Max Normalization
        # batch_data = (batch_data - self.min_feature)/(self.max_feature - self.min_feature)
        # num = batch_data-self.min_feature
        # den = self.max_feature-self.min_feature
        # batch_data = np.divide(num, den, out=np.zeros_like(num), where=den!=0.0)

        # Append zero to the start of the sequence
        packet_zero = np.zeros((batch_data.shape[0],1,batch_data.shape[2]))
        batch_data = np.concatenate((packet_zero, batch_data), axis=1)

        # Split the data into inputs and targets
        batch_inputs = batch_data[:,:-1,:]
        batch_targets = batch_data[:,1:,:]

        if self.return_seq_len:
            return (batch_inputs, batch_targets, batch_seq_len)

        return (batch_inputs, batch_targets)

    def on_epoch_end(self):
        shuffle(self.byte_offset)


from matplotlib import pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms
import datetime
from torchvision import datasets

def data_prepare(dataset_name, n_devices, n_train, n_val, n_labels=2, batch_size=64, rd_seed=111):
    """
    Return the train_loader and devices_train_list
    based on the dataset_name.
    """
    # Input:
    #   dataset_name: A string, should be one of
    #   {"MNIST", "KMNIST", "FMNIST"}
    #   n_devices: number of devices, integer
    #   n_samples: number of samples per device, integer
    #   n_labels: number of labels chosen for each device
    #   batch_size: batch size for creating mini batched
    #               train loader
    # Return:
    #   (train_loader_list, devices_train_list)
    np.random.seed(rd_seed)

    if dataset_name == "MNIST":
        data_path = '../../data/mnist/'
        transform_data = datasets.MNIST(
            data_path, train=True, download=True,
            transform=transforms.Compose([
                transforms.Grayscale(),
                transforms.ToTensor(),
                transforms.Normalize((0.1307),(0.3081))
            ]))
    elif dataset_name == "KMNIST":
        data_path = '../../data/kmnist/'
        transform_data = datasets.KMNIST(
            data_path, train=True, download=True,
            transform=transforms.Compose([
                transforms.Grayscale(),
                transforms.ToTensor(),
                transforms.Normalize((0.1918),(0.3483))
            ]))
    elif dataset_name == "FMNIST":
        data_path = '../../data/fmnist/'
        transform_data = datasets.FashionMNIST(
            data_path, train=True, download=True,
            transform=transforms.Compose([
                transforms.Grayscale(),
                transforms.ToTensor(),
                transforms.Normalize((0.2861),(0.3530))
            ]))

    devices_train_list = []  # list of training data for devices
    train_loader_list = []  # list of train loader for devices
    devices_val_list = []  # list of validation data for devices
    val_loader_list = []  # list of validation loader for devices
    for m in range(n_devices):
        # Choose labels for this device
        labels_choose = list(np.random.choice(10, size=n_labels))

        # Choose training data based on chosen labels
        device_data = []
        for img, label in transform_data:
            if label in labels_choose:
                device_data.append((img / torch.norm(img.squeeze(0)).item(), label))
            #if len(device_data) >= n_train + int((1/2) * n_val):
            if len(device_data) >= n_train + n_val:
                break
        devices_train_list.append(device_data[:n_train])
        train_loader = torch.utils.data.DataLoader(device_data[:n_train], batch_size=batch_size, shuffle=False)
        train_loader_list.append(train_loader)
        # # Choose the other half of validation data
        # # from labels not seen before
        # for img, label in transform_data:
        #     # If the label is not seen, then it has 0.1 prob to
        #     # be included
        #     if label not in labels_choose:
        #         coin = np.random.choice([0,1], p=(0.9, 0.1))
        #         if coin == 1:
        #             device_data.append((img / torch.norm(img.squeeze(0)).item(), label))
        #     if len(device_data) >= n_train + n_val:
        #         break
        devices_val_list.append(device_data[n_train:])
        val_loader = torch.utils.data.DataLoader(device_data[n_train:], batch_size=batch_size, shuffle=False)
        val_loader_list.append(val_loader)

    return train_loader_list, devices_train_list, val_loader_list, devices_val_list


def sensorRGB(sensor):
    sensor.reset()
    sensor.set_pixformat(sensor.RGB565)
    sensor.set_framesize(sensor.QVGA)
    sensor.skip_frames(time = 2000)
    sensor.set_auto_gain(False) # must be turned off for color tracking
    sensor.set_auto_whitebal(False) # must be turned off for color tracking
    return sensor

# Normalize the distance and the color together. Must be 5 chars
def normalize_feedback(dist, color):
    dist = str(dist)
    if len(dist) > 4:
        # Distance is only good up to 3000mm (~10ft).
        mic_str == "n9999"
    elif len(dist) == 4:
        mic_str = color + dist
    elif len(dist) == 3:
        mic_str = color + "0" + dist
    mic_str = '0' + micr_str + '\0'
    return mic_str

def distCalc(b):
    # Calculate distance and return it. This is based upon a 14"x14" box.
    return int(((lens_mm * box_height_mm * image_height_pixels) / (b[3] * sensor_h_mm)) - offset_mm)

def objectDetection(curr_threshold, color):
    # Begin looking for blobs/boxes.
    while(True):
        clock.tick()
        img = sensor.snapshot()
        cam.add_frame(img)
        # For every blob it detects...
        for blob in img.find_blobs(curr_threshold, area_threshold=300, merge=True, margin=10):
            # Draw rect and cross to identify blob currently being tracked.
            img.draw_rectangle(blob.rect())
            img.draw_cross(blob.cx(), blob.cy())

            # Get distance
            distance = distCalc(blob)

            # Normalize values and return to microcontroller.
            mic_str = normalize_feedback(distance, color)
            uart.write(mic_str)

            # Check if micro doesn't need to check for this box anymore
            message = uart.read(1)
            if message == "1":
                led_indicate() # Flip LEDs to indicate succesful run
                return # Go back to 'main' to continue code

import sensor, image, time
import time
from pyb import UART
uart = UART(3, 9600, timeout_char=100)

# Initilization for grayscale for QR Code
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((640//2 - 200//2, 480//2 -200//2, 200, 200))
sensor.skip_frames(10)
sensor.set_auto_gain(False)
clock = time.clock()
message = 'N'

# Variables needed for distance calculations.
lens_mm = 1.7 # Wide angle lens value
box_height_mm = 355.6 # 14" to mm
image_height_pixels = 240.0 # QVGA # Currently runs on QVGA, 120x160
sensor_h_mm = 2.952 # For OV7725 sensor - see datasheet.
offset_mm = -60.0 # Offset fix... Think this is a correction (+b)

print("Hi")

#"[[38.893115, -104.802444], [38.8930933, -104.802345], [38.8932432, -104.8022577]]

while(True):
    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(1.0)
    for code in img.find_qrcodes():
        img.draw_rectangle(code.rect(), color = (255, 0, 0))
        myArray = code.payload()

        str = myArray[7:]
        qr1 = str[1:80]

        message = 'N'
        while message != b'0':
            print("Cord1")
            print(qr1)
            uart.write((qr1))
            print(qr1)
            message = uart.read(1)
            print(message)

    break # Break out of while loop

    print("Finished with QR, Move to Blob")

    sensorRGB(sensor)

    # Set thresholds
    purp_thresh = []
    red_thresh = []

    objectDetection(purp_thresh, 'p')
    objectDetection(purp_thresh, 'p')
    objectDetection(red_thresh, 'r')

    #cord = "[38.893115, -104.802444]"
    #uart.write(str(cord))
    #time.sleep(1000)


#!/usr/bin/env python3
#
# Copyright (c) 2016-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

import json
import subprocess
import typing

from .find_executables import FindExe

def getxattr(abspath: str, attr: str) -> str:
    raw_stdout = subprocess.check_output(
        [FindExe.FSATTR, "--attrName", attr, "--fileName", abspath]
    )
    result = json.loads(raw_stdout)
    # fsattr should always return a string here.  We just cast the result,
    # without actually validating it for now.
    return typing.cast(str, result)

def listxattr(abspath: str) -> typing.Dict[str, str]:
    raw_stdout = subprocess.check_output([FindExe.FSATTR, "--fileName", abspath])
    result = json.loads(raw_stdout)
    # fsattr should always return a dictionary here.  We just cast the
    # result, without actually validating it for now.
    return typing.cast(typing.Dict[str, str], result)


import FWCore.ParameterSet.Config as cms

process = cms.Process("PRINT")

process.options = cms.untracked.PSet(
  numberOfThreads = cms.untracked.uint32( 1 ),
  numberOfStreams = cms.untracked.uint32( 1 ),
  wantSummary = cms.untracked.bool( False )
)

process.source = cms.Source("PoolSource",
  fileNames = cms.untracked.vstring("file:cylindricalVectors.root"),
)

process.printCylindricalVectors = cms.EDAnalyzer('PrintCylindricalVectors',
  input = cms.InputTag('generateCylindricalVectors')
)

process.path = cms.Path(process.printCylindricalVectors)

process.maxEvents = cms.untracked.PSet(
  input = cms.untracked.int32( 1 )
)


# def calcular_doble(numero):
#     print(numero*2)

# calcular_doble(10)
# calcular_doble('hi ') #Sirve también para multiplicar str, los repite

# def calculo(dato:int):
#     print(dato*3) #la funcion de esto es utilizar el parametro dat, dato no va a servir mas fuera de este espacio

# calculo(int(input('Ingresa un dato: ')))    #Memultiplica un numero
# calculo(input('Ingresa un dato: '))         #Me multiplica un str

# def total(n1, n2, n3):
#     return n1+n2+n3

# total(5,4,4)

def tiempo(entraron, salieron):
    tiempo_in = entraron*2
    tiempo_out = salieron*1
    tiempo_viaje = (90/30)*60
    return tiempo_in+tiempo_out+tiempo_viaje

print('El bus se demora en llegar', round((tiempo(int(input('Ingrese el numero de pasajeros que ingresaron: ')), (int(input('Ingrese el numero de pasajeros que se bajaron: '))))/60),2), 'horas')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jul  4 17:31:03 2020

@author: xuel12
"""

import base64
import io
import os
# import sys
import re
import glob
import time
from urllib.parse import quote as urlquote
import subprocess

from flask import Flask, send_from_directory
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
from dash_extensions.callback import DashCallbackBlueprint

# from app import App, build_graph
from homepage import Homepage
from train import Training
from eda import EDA

import pandas as pd
import pickle

BASE_DIR = "/Users/xuel12/Documents/MSdatascience/DS5500datavis/project2/"
CODE_DIR = BASE_DIR+"h1permprediction/"
os.chdir(CODE_DIR)

import constants

os.chdir(constants.CODE_DIR)
base_path = constants.BASE_PATH
input_dir = constants.INPUT_DIR
if not os.path.exists(input_dir):
    os.makedirs(input_dir)
temp_dir = constants.TEMP_DIR
if not os.path.exists(temp_dir):
    os.makedirs(temp_dir)
model_dir = constants.MODEL_DIR
if not os.path.exists(model_dir):
    os.makedirs(model_dir)
download_dir = constants.DOWNLOAD_DIR
if not os.path.exists(download_dir):
    os.makedirs(download_dir)

dcb = DashCallbackBlueprint()

# # Normally, Dash creates its own Flask server internally. By creating our own,
# # we can create a route for downloading files directly:
server = Flask(__name__)

# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
external_stylesheets=[dbc.themes.BOOTSTRAP]

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# app = dash.Dash(__name__, external_stylesheets=[dbc.themes.UNITED])

app.config.suppress_callback_exceptions = True

@server.route(constants.DOWNLOAD_DIR + "<path:path>")
def download(path):
    """Serve a file from the upload directory."""
    return send_from_directory(input_dir, path, as_attachment=True)

app.layout = html.Div([
    dcc.Location(id = 'url', refresh = False),
    html.Div(id = 'page-content')
])

@app.callback(Output('page-content', 'children'),
            [Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/training':
        return Training()
    elif pathname == '/eda':
        return EDA()
    else:
        return Homepage()

@app.callback(
    Output("file-list", "children"),
    [Input("upload-data", "filename"), Input("upload-data", "contents")],
)
def update_output(uploaded_filenames, uploaded_file_contents):
    """Save uploaded files and regenerate the file list."""

    if uploaded_filenames is not None and uploaded_file_contents is not None:
        for name, data in zip(uploaded_filenames, uploaded_file_contents):
            save_file(name, data)

    files = uploaded_files()
    if len(files) == 0:
        return [html.Li("No files yet!")]
    else:
        return [html.Li(file_download_link(filename)) for filename in files]

@app.callback(
    [Output('start-indicator', 'color'), Output("submiting-data", "children")],
    [Input("submit-data", "n_clicks")])
def start_indicator(n_clicks):
    if n_clicks:
        time.sleep(1)
    if n_clicks % 2 == 1:
        color = 'blue'
    else:
        color = 'grey'
    return color, ""

@app.callback(
    Output('csvreader-status', 'value'),
    # [Output("progress", "value"), Output("progress", "children")],
    # specify the component and its property that shall contain the output
    [Input('start-indicator', 'color')],
    # specify the component and corresponding properties that shall serve as input
    [State('input-on-submit','value')])
    # specify the component and corresponding properties that shall serve as input
def update_data(color, base_path):  # define the function reaching output from input
    if color == 'blue':
        # BASE_PATH = value  # input value gives the base directory
        input_dir = folderStruct(base_path)['input_dir']

        # convert xlsx to csv for faster process
        count_newcsv = xlsx2csv(input_dir)
        # count_newcsv = 0
        # return progress, f"{progress} %" if progress >= 5 else ""
        return count_newcsv
    else:
        return -1

@app.callback(
    Output('xlsx2csv-indicator', 'color'),
    # specify the component and its property that shall contain the output
    [Input('csvreader-status', 'value')])
def xlsx2csv_indicator(status):
    if status != -1:
        color = 'orange'
    else:
        color = 'grey'
    return color

@app.callback(
    [Output('parsing status', 'children'), Output('combinecsv-status', 'value')],
    # specify the component and its property that shall contain the output
    [Input('csvreader-status', 'value')],
    [State('input-on-submit','value')])
def update_combinedata(count_newcsv, value):  # define the function reaching output from input
    if count_newcsv != -1:
        temp_dir = folderStruct(base_path)['temp_dir']
        input_dir = folderStruct(base_path)['input_dir']
        header_dir = folderStruct(base_path)['header_dir']
        outputfile = 'h1b2015to2020.csv'
        headerfile = 'headers.csv'

        # read in csv to dataframe
        if count_newcsv > 0 or not os.path.exists(temp_dir + outputfile):
            csvCombine(input_dir, temp_dir, header_dir, outputfile, headerfile)

        makeEDAreports(outputfile, temp_dir)

        finish_message = 'Data parsing complete, find the parsed combineCSV in directory'
        # return progress, f"{progress} %" if progress >= 5 else ""
        return finish_message, 'done'
    else:
        return '', 'wait'

@app.callback(
    Output('csvcombine-indicator', 'color'),
    # specify the component and its property that shall contain the output
    [Input('combinecsv-status', 'value')])
def csvcombine_indicator(status):
    if status == 'done':
        color = 'red'
    else:
        color = 'grey'
    return color

@app.callback(
    [Output("progress", "value"), Output("progress", "children")],
    [Input('start-indicator', 'color'),
      Input('xlsx2csv-indicator', 'color'), Input('csvcombine-indicator', 'color')],
)
def data_progress(start_color, xlsx2csv_indicator, csvcombine_indicator):
    # check progress of some background process, in this example we'll just
    # use n_intervals constrained to be in 0-100
    if csvcombine_indicator != 'grey':
        progress = 100
    elif xlsx2csv_indicator != 'grey':
        progress = 70
    elif start_color != 'grey':
        progress = 20
    else:
        progress = 0

    # only add text after 5% progress to ensure text isn't squashed too much
    return progress, f"{progress} %" if progress >= 5 else ""

def save_file(filename, content):
    content_type, content_string = content.split(',')

    decoded = base64.b64decode(content_string)
    try:
        filename_csv = os.path.splitext(filename)[0]+'.csv'
        if not os.path.exists(input_dir+filename_csv):
            if 'csv' in filename:
                # Assume that the user uploaded a CSV file
                df = pd.read_csv(io.StringIO(decoded.decode('utf-8')))
            elif 'xlsx' in filename:
                # Assume that the user uploaded an excel file
                df = pd.read_excel(io.BytesIO(decoded))
            df.to_csv(input_dir+filename_csv, index=False)
            print('File added to database.')
        else:
            print('File exists.')
    except Exception as e:
        print(e)
        return html.Div([
            'There was an error processing this file.'
        ])

def uploaded_files():
    """List the files in the upload directory."""
    files = []
    for filename in os.listdir(input_dir):
        path = os.path.join(input_dir, filename)
        if os.path.isfile(path):
            files.append(filename)
    return files

def file_download_link(filename):
    """Create a Plotly Dash 'A' element that downloads a file from the app."""
    location = download_dir+"{}".format(urlquote(filename))
    return html.A(filename, href=location)

def folderStruct(BASE_PATH):
    CODE_DIR = BASE_PATH + "h1permprediction/"
    INPUT_DIR = BASE_PATH + "input/"
    # PREDICT_DIR = BASE_PATH + "predict/"
    TEMP_DIR = BASE_PATH + "temp/"
    MODEL_DIR = BASE_PATH + "model/"
    # OUTPUT_DIR = BASE_PATH + "output/"
    DOWNLOAD_DIR = BASE_PATH + "download/"
    HEADER_DIR = BASE_PATH + 'header/'

    os.chdir(CODE_DIR)
    # base_path = BASE_PATH
    input_dir = INPUT_DIR
    if not os.path.exists(input_dir):
        os.makedirs(input_dir)
    temp_dir = TEMP_DIR
    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)
    model_dir = MODEL_DIR
    if not os.path.exists(model_dir):
        os.makedirs(model_dir)
    download_dir = DOWNLOAD_DIR
    if not os.path.exists(download_dir):
        os.makedirs(download_dir)
    header_dir = HEADER_DIR
    if not os.path.exists(header_dir):
        os.makedirs(header_dir)

    return {'input_dir':input_dir, 'temp_dir':temp_dir, 'model_dir':model_dir,
            'download_dir':download_dir, 'BASE_PATH':BASE_PATH, 'CODE_DIR':CODE_DIR,
            'header_dir':header_dir}

# convert xlsx to csv
def xlsx2csv(in_dir):
    xlsx_path = in_dir
    csv_path = in_dir
    list_of_xlsx = glob.glob(xlsx_path+'*.xlsx')

    count_newcsv = 0
    for xlsx in list_of_xlsx:
        # Extract File Name on group 2 "(.+)"
        filename = re.search(r'(.+[\\|\/])(.+)(\.(xlsx))', xlsx).group(2)
        if not os.path.exists(csv_path+filename+'.csv'):
            # Setup the call for subprocess.call()
            call = ["python", "./xlsx2csv.py", xlsx, csv_path+filename+'.csv']
            try:
                subprocess.call(call) # On Windows use shell=True
            except:
                print('Failed with {}'.format(xlsx_path))
            count_newcsv = count_newcsv + 1
    return count_newcsv

# read each csv to df and then concatenate
# def csvCombine(in_dir, temp_dir):
#     outputcsv = temp_dir+'bigcsv.csv' #specify filepath+filename of output csv
#     csv_path = in_dir

#     listofdataframes = []
#     for file in glob.glob(csv_path+'*.csv'):
#         df = pd.read_csv(file)
#         if df.shape[1] > 0: # make sure there are columns
#             listofdataframes.append(df)
#         else:
#             print('{} has {} columns - skipping'.format(file,df.shape[1]))
#     bigdataframe = pd.concat(listofdataframes).reset_index(drop=True)
#     bigdataframe.to_csv(outputcsv,index=False)

def csvCombine(in_dir, temp_dir, header_dir, outputfile, headerfile):
    # outputfile = temp_dir+outputfile    #specify filepath+filename of output csv

    # use header mapping file for parsing
    headers_df = pd.read_csv(header_dir+headerfile, index_col=0)

    # loop through all csv files in header file
    listofdataframes = []
    # csvfilenames = 'H-1B_Disclosure_Data_FY15_Q4'
    for csvfilenames in headers_df.to_dict().keys():
        csvfile = input_dir + csvfilenames + '.csv'
        if os.path.exists(csvfile):
            headers_temp = headers_df[csvfilenames]
            df = pd.read_csv(csvfile, usecols=headers_temp.dropna(),
                      dtype='str',
                      parse_dates=[headers_temp['CASE_SUBMITTED']]).dropna(how='all')
            # filter rows, Remove "Withdraw" and "Certified Expired"
            df = df[((df['CASE_STATUS'].str.upper() == 'CERTIFIED') | \
                      (df['CASE_STATUS'].str.upper() == 'DENIED')) & \
                    (df['VISA_CLASS'].str.upper() == 'H-1B')]

            # Similarly, most of employer come from the U.S.. We only keep application with American employer
            df = df[df.EMPLOYER_COUNTRY == 'UNITED STATES OF AMERICA']

            headers_temp_dict = {y:x for x,y in headers_temp.dropna().items()}
            df = df.rename(columns=headers_temp_dict)
            if df.shape[1] > 0: # make sure there are columns
                listofdataframes.append(df)
                print(csvfilenames + ' Done')
            else:
                print('{} has {} columns - skipping'.format(csvfilenames,df.shape[1]))
    df = pd.concat(listofdataframes).reset_index(drop=True)

    # uppercase all string for consistency
    df = df.apply(lambda x: x.astype(str).str.upper())
    df['CASE_SUBMITTED'] = pd.to_datetime(df['CASE_SUBMITTED'])
    # df1 = df.fillna(value={'PREVAILING_WAGE': 0.0})
    # df2 = df1[df1["PREVAILING_WAGE"] == 'NAN']

    # mapping value based on mapping file
    df['PW_WAGE_LEVEL'] = df['PW_WAGE_LEVEL'].replace(constants.PW_WAGE_LEVEL_MAP)
    df = df.replace({'PW_WAGE_LEVEL': constants.PW_WAGE_LEVEL_MAP,
                        'EMPLOYER_STATE': constants.US_STATE_ABBREV,
                        'WORKSITE_STATE': constants.US_STATE_ABBREV,
                        "PREVAILING_WAGE": {'NAN': -1},
                        "PW_UNIT_OF_PAY": {'NAN': 'UNKNOWN'},
                        })
    df["PREVAILING_WAGE"] = pd.to_numeric(df["PREVAILING_WAGE"], downcast="float")
    df = df.replace({'NAN': 'UNKOWN'})

    # feature engineer on jobs
    df["EMPLOYER_NAME"]=df["EMPLOYER_NAME"].str.replace("INC.","INC")
    df['JOB_CATEGORY']=df['SOC_CODE'].apply(lambda x: jobClassifier(x))
    df['JOB_LEVEL']=df['JOB_TITLE'].apply(lambda x: levelClassifier(x))

    df.to_csv(temp_dir+outputfile, index=False)

    print('There are {} records.'.format(df.shape[0]))

    # return df

def jobClassifier(soc_code):
    soc_map = constants.SOC_MAP
    soc = str(soc_code).split('-')[0]
    return soc_map.get(soc,'OTHER')

def levelClassifier(job_title):
    job_title = str(job_title)
    if job_title.find('SENIOR')!=-1 or job_title.find(' II')!=-1 or job_title.find('2')!=-1 or job_title.find('3')!=-1:
        return 'SENIOR'
    elif job_title.find('JUNIOR')!=-1 or job_title.find(' I')!=-1 or job_title.find('1')!=-1:
        return 'JUNIOR'
    else:
        return 'OTHER'

def makeEDAreports(csvfile, temp_dir):
    csvfile = 'h1b2015to2020_sub.csv'
    df = pd.read_csv(temp_dir + csvfile, parse_dates=['CASE_SUBMITTED'])

    edaplot = {}
    edaplot['EMPLOYER_STATE'] = df.groupby('EMPLOYER_STATE').count()
    edaplot['WORKSITE_STATE'] = df.groupby('WORKSITE_STATE').count()
    edaplot['JOB_CATEGORY'] = df.groupby('JOB_CATEGORY').count()
    edaplot['JOB_LEVEL'] = df.groupby('JOB_LEVEL').count()
    edaplot['FULL_TIME_POSITION'] = df.groupby('FULL_TIME_POSITION').count()
    edaplot['PW_WAGE_LEVEL'] = df.groupby('PW_WAGE_LEVEL').count()
    edaplot['H-1B_DEPENDENT'] = df.groupby('H-1B_DEPENDENT').count()
    edaplot['WILLFUL_VIOLATOR'] = df.groupby('WILLFUL_VIOLATOR').count()
    edaplot['CASE_SUBMITTED'] = (df.groupby(['CASE_STATUS', pd.Grouper(key='CASE_SUBMITTED', freq='M')])['JOB_CATEGORY']
        .count().reset_index().pivot(index='CASE_SUBMITTED', columns='CASE_STATUS', values='JOB_CATEGORY'))

    pickle_out = open(temp_dir+"eda.pickle","wb")
    pickle.dump(edaplot, pickle_out)
    pickle_out.close()

# @app.callback(
#     Output('output', 'children'),
#     [Input('pop_dropdown', 'value')]
# )
# def update_graph(city):
#     graph = build_graph(city)
#     return graph

if __name__ == '__main__':
    app.run_server(debug=True)


import requests
from requests import get
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

def get_data(url):
    # response = get(url, timeout=10)

    # if response.status_code >= 400:
    #     raise RuntimeError(f'Request failed: { response.text }')

    retry_strategy = Retry(
        total=10,
        status_forcelist=[429, 500, 502, 503, 504],
        method_whitelist=["HEAD", "GET", "OPTIONS"]
    )
    adapter = HTTPAdapter(max_retries=retry_strategy)
    http = requests.Session()
    http.mount("https://", adapter)
    http.mount("http://", adapter)

    response = http.get(url)

    return response.json()

def gdc(areaType,areaName):
    if areaType == 'Nation':
        endpoint = (
            'https://api.coronavirus.data.gov.uk/v1/data?'
            'filters=areaType=' +
            areaType
            +';areaName='+
            areaName
            +'&structure={"date":"date","newCasesByPublishDate":"newCasesByPublishDate"}'
        )
    else:
        endpoint = (
            'https://api.coronavirus.data.gov.uk/v1/data?'
            'filters=areaType=' +
            areaType
            +';areaName='+
            areaName
            +'&structure={"date":"date","newCasesByPublishDate":"newCasesByPublishDate"}'
        )

    data = get_data(endpoint)

    return data


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

import os
import platform
import sys
from enum import Enum

execProgStr = sys.argv[1]

class DataSourceFileType(Enum):
    noinput = 0
    input = 1
    readtxt = 2
    readdat = 3

class ResultFileType(Enum):
    nooutput = 0
    output = 1
    writetxt = 2
    writedat = 3

class PlatformType(Enum):
    other = 0
    windows = 1
    linux = 2

# 读取文件
def read_file(file_name):
    try:
        file_desc = open(file_name, 'r')
        # 读取后按行分割
        text = file_desc.read().splitlines()
        file_desc.close()
        return text
    except IOError as error:
        print('Read input file Error: {0}'.format(error))
        sys.exit()

# 比较时忽略行末的空格和文件末尾的回车换行
def advanced_file_compare(file1, file2):
    f1 = open(file1)
    f2 = open(file2)

    returnVal = 1

    str1 = []
    for line1 in f1:
        line1 = line1.rstrip()
        line1 = line1.replace('\n', '')
        if len(line1) != 0:
            str1.append(line1)
    str2 = []
    for line2 in f2:
        line2 = line2.rstrip()
        line2 = line2.replace('\n', '')
        if len(line2) != 0:
            str2.append(line2)

    count = 0
    if len(str1) == len(str2):
        for line1 in str1:
            if line1 != str2[count]: #文件不同
                returnVal = 0
                break
            else:
                count = count + 1
    else:
        returnVal = 0

    f1.close()
    f2.close()

    return returnVal

def advanced_dat_file_compare(file1, file2):
    returnVal = 1
    runCommand = "diff {0} {1} -b -B -y -i -W 100".format(file1, file2)
    execResult = os.system(runCommand)
    if execResult != 0:
        returnVal = 0
    return returnVal

def getPlatformType():
    platformType = PlatformType.other
    if platform.system().lower() == 'windows':
        platformType = PlatformType.windows
    elif platform.system().lower() == 'linux':
        platformType = PlatformType.linux
    else:
        platformType = PlatformType.other
    return platformType

def getProjInfo():
    dsType = getDataSourceFileType()
    dsFileStr = getDataSourceFileStr(dsType)
    resultFileType = getResultFileType()
    resultFileStr = getResultFileStr(resultFileType)
    userresultFileStr = getUserResultFileStr(resultFileType)
    return dsType, dsFileStr, resultFileType, userresultFileStr, resultFileStr

def getDataSourceFileType():
    dsType = DataSourceFileType.noinput
    if os.path.isfile("input1.txt"):
        dsType = DataSourceFileType.input
    elif os.path.isfile("readfile1.txt"):
        dsType = DataSourceFileType.readtxt
    elif os.path.isfile("readfile1.dat"):
        dsType = DataSourceFileType.readdat
    else:
        dsType = DataSourceFileType.noinput
    return dsType

def getDataSourceFileStr(dataSourceFileType):
    dsType = dataSourceFileType
    dsFileStr = ""
    if dsType == DataSourceFileType.readtxt:
        dsFileStr = "readfile{0}.txt"
    elif dsType == DataSourceFileType.readdat:
        dsFileStr = "readfile{0}.dat"
    elif dsType == DataSourceFileType.input:
        dsFileStr = "input{0}.txt"
    return dsFileStr

def getResultFileType():
    resultFileType = 0
    if os.path.isfile("output1.txt"):
        resultFileType = ResultFileType.output
    elif os.path.isfile("writefile1.txt"):
        resultFileType = ResultFileType.writetxt
    elif os.path.isfile("writefile1.dat"):
        resultFileType = ResultFileType.writedat
    else:
        resultFileType = ResultFileType.nooutput
    return resultFileType

def getResultFileStr(resultFileType):
    resultFileStr = ""
    type = resultFileType
    if type == ResultFileType.output:
        resultFileStr = "output{0}.txt"
    elif type == ResultFileType.writetxt:
        resultFileStr = "writefile{0}.txt"
    elif type == ResultFileType.writedat:
        resultFileStr = "writefile{0}.dat"
    return resultFileStr

def getUserResultFileStr(resultFileType):
    userresultFileStr = ""
    type = resultFileType
    if type == ResultFileType.output:
        userresultFileStr = "user_output{0}.txt"
    elif type == ResultFileType.writetxt:
        userresultFileStr = "user_writefile{0}.txt"
    elif type == ResultFileType.writedat:
        userresultFileStr = "user_writefile{0}.dat"
    return userresultFileStr

def getSpecifyfileStr():
    specifyFile = ""
    if os.path.isfile("file.dat"):
        specifyFile = "file.dat"
    elif os.path.isfile("file.txt"):
        specifyFile = "file.txt"
    return specifyFile

def getRunProgCmdStr(dsType, resultFileType, dsFile, userresultFile):
    global execProgStr
    inresultFileStr = ""
    if dsType == DataSourceFileType.input:
        if resultFileType == ResultFileType.output:
            inresultFileStr = " < {0} > {1} ".format(dsFile, userresultFile)
        elif resultFileType == ResultFileType.writedat or resultFileType == ResultFileType.writetxt:
            inresultFileStr = " {1} < {0} ".format(dsFile, userresultFile)
    elif dsType == DataSourceFileType.readtxt or dsType == DataSourceFileType.readdat:
        if resultFileType == ResultFileType.output:
            inresultFileStr = " {0} > {1} ".format(dsFile, userresultFile)
        elif resultFileType == ResultFileType.writedat or resultFileType == ResultFileType.writetxt:
            inresultFileStr = " {0} {1} ".format(dsFile, userresultFile)
    else:
        if resultFileType == ResultFileType.output:
            inresultFileStr = " > {0} ".format(userresultFile)
        elif resultFileType == ResultFileType.writedat or resultFileType == ResultFileType.writetxt:
            inresultFileStr = " {0} ".format(userresultFile)

    runCommandStr = execProgStr
    runCommandStr += inresultFileStr
    return runCommandStr

def runCmd(cmdStr):
    print("正在执行命令: {0}".format(cmdStr))
    sys.stdout.flush()
    execResult = os.system(cmdStr)
    if execResult != 0:
        exit(1)


from abc import ABC, abstractmethod
from typing import Mapping, Dict, Optional, Union, Any
import json

from API.jikanpy.jikanpy.exceptions import APIException, ClientException, DeprecatedEndpoint
from API.jikanpy.jikanpy.parameters import (
    EXTENSIONS,
    SEARCH_PARAMS,
    SEASONS,
    DAYS,
    SUBTYPES,
    GENRE_TYPES,
    CREATOR_TYPES,
    USER_REQUESTS,
    USER_HISTORY_ARGUMENTS,
    USER_ANIMELIST_ARGUMENTS,
    USER_MANGALIST_ARGUMENTS,
    META,
)

BASE_URL: str = "http://api.jikan.moe/v3/"
BASE_URL_SSL: str = "https://api.jikan.moe/v3/"

class AbstractJikan(ABC):
    """
    Base class for wrapper for calls to the jikan.moe unofficial MyAnimeList API.

    Note that the API has a limit of 30 requests/minute and 2 requests/second;
    this module does not make any effort to prevent abuse of that limit,
    so use it responsibly.
    """

    def __init__(
        self, selected_base: Optional[str] = None, use_ssl: bool = True
    ) -> None:
        super().__init__()
        if selected_base is None:
            selected_base = BASE_URL_SSL if use_ssl else BASE_URL
        self.base: str = selected_base + "{endpoint}/{id}"
        self.search_base: str = selected_base + "search/{search_type}?q={query}"
        self.season_base: str = selected_base + "season/{year}/{season}"
        self.schedule_base: str = selected_base + "schedule"
        self.top_base: str = selected_base + "top/{type}"
        self.meta_base: str = selected_base + "meta/{request}"
        self.genre_base: str = selected_base + "genre/{type}/{genre_id}"
        self.creator_base: str = selected_base + "{creator_type}/{creator_id}"
        self.user_base: str = selected_base + "user/{username}"
        self.season_archive_url: str = selected_base + "season/archive"
        self.season_later_url: str = selected_base + "season/later"

    def _check_response(
        self, response: Any, **kwargs: Union[int, Optional[str]]
    ) -> None:
        """
        Check if the response is an error

        Keyword Arguments:
        response -- response from the API call
        kwargs -- keyword arguments
        """
        if response.status_code >= 400:
            try:
                json_resp = response.json()
                error_msg = json_resp.get("error")
            except json.decoder.JSONDecodeError:
                error_msg = ""
            err_str: str = "{} {}: error for ".format(response.status_code, error_msg)
            err_str += ", ".join("=".join((str(k), str(v))) for k, v in kwargs.items())
            raise APIException(err_str)

    def _get_url(
        self, endpoint: str, id: int, extension: Optional[str], page: Optional[int]
    ) -> str:
        """Create URL for anime, manga, character, person, and club endpoints"""
        url: str = self.base.format(endpoint=endpoint, id=id)
        # Check if extension is valid
        if extension is not None:
            if extension not in EXTENSIONS[endpoint]:
                raise ClientException("The extension is not valid")
            url += "/" + extension
            if extension in ("episodes", "reviews", "userupdates", "members"):
                url = self._add_page_to_url(url, page)
        return url

    def _get_search_url(
        self,
        search_type: str,
        query: str,
        page: Optional[int],
        parameters: Optional[Mapping[str, Optional[Union[int, str, float]]]],
    ) -> str:
        """Create URL for search endpoint"""
        url: str = self.search_base.format(search_type=search_type, query=query)
        url = self._add_page_to_url(url, page, delimiter="&page=")
        if parameters is not None:
            if search_type.lower() not in ("anime", "manga"):
                raise ClientException("Parameters are only for anime or manga search")
            url += "&"
            for key, value in parameters.items():
                if key == "rated" and search_type.lower() == "manga":
                    raise ClientException("rated parameter only for anime search")
                if key in ("type", "status", "rated", "genre", "order_by"):
                    values = SEARCH_PARAMS[search_type][key]  # type: ignore
                else:
                    values = SEARCH_PARAMS.get(key)
                if values is None:
                    raise ClientException("The key is not valid")
                elif key == "genre" and isinstance(value, str):
                    genres = value.split(",")
                    for genre in genres:
                        if int(genre) not in values:
                            raise ClientException("Invalid genre passed in")
                elif isinstance(values, tuple) and value not in values:
                    raise ClientException("The value is not valid")
                url += key + "=" + str(value) + "&"
        return url

    def _get_season_url(self, year: int, season: str) -> str:
        """Create URL for season endpoint"""
        url: str = self.season_base.format(year=year, season=season.lower())
        # Check if year and season are valid
        if not (isinstance(year, int) and season.lower() in SEASONS):
            raise ClientException("Season or year is not valid")
        return url

    def _get_schedule_url(self, day: Optional[str]) -> str:
        """Create URL for schedule endpoint"""
        url: str = self.schedule_base
        # Check if day is valid
        if day is not None:
            if day.lower() not in DAYS:
                raise ClientException("Day is not valid")
            else:
                url += "/" + day.lower()
        return url

    def _get_top_url(
        self, type: str, page: Optional[int], subtype: Optional[str]
    ) -> str:
        """Create URL for top endpoint"""
        url: str = self.top_base.format(type=type.lower())
        # Check if type is valid
        if type.lower() not in SUBTYPES:
            raise ClientException("Type must be anime or manga")
        url = self._add_page_to_url(url, page)
        # Check if subtype is valid
        if subtype is not None:
            if page is None:
                raise ClientException("Page is required if subtype is given")
            if subtype.lower() not in SUBTYPES[type.lower()]:
                raise ClientException("Subtype is not valid for " + type)
            url += "/" + subtype.lower()
        return url

    def _get_genre_url(self, type: str, genre_id: int, page: Optional[int]) -> str:
        """Create URL for genre endpoint"""
        url: str = self.genre_base.format(type=type.lower(), genre_id=genre_id)
        # Check if type is valid
        if type.lower() not in GENRE_TYPES:
            raise ClientException("Type must be anime or manga")
        if not isinstance(genre_id, int):
            raise ClientException("ID must be an integer")
        url = self._add_page_to_url(url, page)
        return url

    def _get_creator_url(
        self, creator_type: str, creator_id: int, page: Optional[int]
    ) -> str:
        """Create URL for producer and magazine endpoints"""
        url: str = self.creator_base.format(
            creator_type=creator_type.lower(), creator_id=creator_id
        )
        # Check if type is valid
        if creator_type.lower() not in CREATOR_TYPES:
            raise ClientException("Type must be producer or magazine")
        if not isinstance(creator_id, int):
            raise ClientException("ID must be an integer")
        url = self._add_page_to_url(url, page)
        return url

    def _get_user_url(
        self,
        username: str,
        request: Optional[str],
        argument: Optional[Union[int, str]],
        page: Optional[int],
        parameters: Optional[Mapping],
    ) -> str:
        """Create URL for user endpoint"""
        url: str = self.user_base.format(username=username.lower())
        if request is not None:
            if request not in USER_REQUESTS:
                raise ClientException("Invalid request for user endpoint")
            url += "/" + request
            if request.lower() == "profile" and argument is not None:
                raise ClientException("No argument should be given for profile request")
            if (
                request.lower() in ("animelist", "mangalist")
                and argument is None
                and page is not None
            ):
                raise ClientException(
                    "You must provide an argument if you provide a page for animelist or mangalist"
                )
            if argument is not None:
                if request.lower() == "friends" and not isinstance(argument, int):
                    raise ClientException(
                        "Argument for friends request must be a page number integer"
                    )
                if isinstance(argument, str):
                    if (
                        request.lower() == "history"
                        and argument.lower() not in USER_HISTORY_ARGUMENTS
                    ):
                        raise ClientException(
                            "Argument for history request should be anime or manga"
                        )
                    if (
                        request.lower() == "animelist"
                        and argument.lower() not in USER_ANIMELIST_ARGUMENTS
                    ):
                        raise ClientException(
                            "Argument for animelist request is not valid"
                        )
                    if (
                        request.lower() == "mangalist"
                        and argument.lower() not in USER_MANGALIST_ARGUMENTS
                    ):
                        raise ClientException(
                            "Argument for mangalist request is not valid"
                        )
                url += "/" + str(argument)
                if request.lower() in ("animelist", "mangalist"):
                    url = self._add_page_to_url(url, page)
        if parameters is not None:
            url += "?"
            for key, value in parameters.items():
                url += key + "=" + str(value) + "&"
        return url

    def _get_meta_url(
        self,
        request: str,
        type: Optional[str],
        period: Optional[str],
        offset: Optional[int],
    ) -> str:
        """Create URL for meta endpoint"""
        url: str = self.meta_base.format(request=request)
        # Check if request is valid
        if request not in META["request"]:
            raise ClientException("Request must be 'requests' or 'status'")
        if request == "status" and (
            type is not None or period is not None or offset is not None
        ):
            raise ClientException("There is no type or period for the 'status' request")
        if request == "requests":
            if type not in META["type"]:
                raise ClientException("Type is not valid")
            if period not in META["period"]:
                raise ClientException("Period must be 'today', 'weekly', or 'monthly'")
            url += "/" + type + "/" + period
            url = self._add_page_to_url(url, offset)
        return url

    def _add_page_to_url(
        self, url: str, page: Optional[int], delimiter: str = "/"
    ) -> str:
        """Add page to URL if it exists and is valid"""
        if page is not None:
            if not isinstance(page, int):
                raise ClientException("The parameter 'page' must be an integer")
            url += delimiter + str(page)
        return url

    @abstractmethod
    def _get(
        self,
        endpoint: str,
        id: int,
        extension: Optional[str],
        page: Optional[int] = None,
    ) -> Dict:
        """
        Gets the response from Jikan API given the endpoint

        Keyword Arguments:
        endpoint -- endpoint of API (anime, manga, character, person, club)
        id -- id of what to get the information of
        extension -- special information to get, possible values in the docs
        page -- page number of the results (default None)
        """
        raise NotImplementedError

    @abstractmethod
    def _get_creator(
        self, creator_type: str, creator_id: int, page: Optional[int] = None
    ) -> Dict:
        """Gets the response from Jikan API for producer and magazine"""
        raise NotImplementedError

    def anime(
        self, id: int, extension: Optional[str] = None, page: Optional[int] = None
    ) -> Dict:
        return self._get("anime", id, extension, page)

    def manga(
        self, id: int, extension: Optional[str] = None, page: Optional[int] = None
    ) -> Dict:
        return self._get("manga", id, extension, page)

    def character(self, id: int, extension: Optional[str] = None) -> Dict:
        return self._get("character", id, extension)

    def person(self, id: int, extension: Optional[str] = None) -> Dict:
        return self._get("person", id, extension)

    def club(self, id: int, extension: Optional[str] = None) -> Dict:
        return self._get("club", id, extension)

    def user_list(self, id: int, extension: Optional[str] = None) -> Dict:
        """Gets user list information"""
        raise DeprecatedEndpoint("user_list is a deprecated endpoint")

    @abstractmethod
    def search(
        self,
        search_type: str,
        query: str,
        page: Optional[int] = None,
        parameters: Optional[Mapping[str, Optional[Union[int, str, float]]]] = None,
    ) -> Dict:
        """
        Searches for a query on MyAnimeList

        Keyword Arguments:
        search_type -- where to search (anime, manga, person, character)
        query -- query to search for
        page -- page number of the results (default None)
        parameters - dictionary containing key,value pairs for ?key=value in url query
        """
        raise NotImplementedError

    @abstractmethod
    def season(self, year: int, season: str) -> Dict:
        """
        Gets information on anime of the specific season

        Keyword Arguments:
        year -- year to get anime of
        season -- season to get anime of (winter, spring, summer, fall)
        """
        raise NotImplementedError

    @abstractmethod
    def season_archive(self) -> Dict:
        """
        Gets all the years and their respective seasons from MyAnimeList
        """
        raise NotImplementedError

    @abstractmethod
    def season_later(self) -> Dict:
        """
        Gets anime that have been announced for upcoming seasons
        """
        raise NotImplementedError

    @abstractmethod
    def schedule(self, day: Optional[str] = None) -> Dict:
        """
        Gets anime scheduled for the specific day

        Keyword Arguments:
        day -- day to get anime of (default None)
        """
        raise NotImplementedError

    @abstractmethod
    def top(
        self, type: str, page: Optional[int] = None, subtype: Optional[str] = None
    ) -> Dict:
        """
        Gets top items on MyAnimeList

        Keyword Arguments:
        type -- type to get top items from (anime, manga)
        page -- page number of the results (default None)
        subtype -- subtype to get filtered top items, possible values in docs
        """
        raise NotImplementedError

    @abstractmethod
    def genre(self, type: str, genre_id: int, page: Optional[int] = None) -> Dict:
        """
        Gets anime or manga by genre

        Keyword Arguments:
        type -- type to get items from (anime, manga)
        genre_id -- genre ID from MyAnimeList
        page -- page number of the results (default None)
        """
        raise NotImplementedError

    def producer(self, producer_id: int, page: Optional[int] = None) -> Dict:
        """
        Gets anime by the producer/studio/licensor

        Keyword Arguments:
        producer_id -- producer ID from MyAnimeList
        page -- page number of the results (default None)
        """
        return self._get_creator("producer", producer_id, page)

    def magazine(self, magazine_id: int, page: Optional[int] = None) -> Dict:
        """
        Gets manga by the magazine/serializer/publisher

        Keyword Arguments:
        magazine_id -- magazine ID from MyAnimeList
        page -- page number of the results (default None)
        """
        return self._get_creator("magazine", magazine_id, page)

    @abstractmethod
    def user(
        self,
        username: str,
        request: Optional[str],
        argument: Optional[Union[int, str]] = None,
        page: Optional[int] = None,
        parameters: Optional[Mapping] = None,
    ) -> Dict:
        """
        Gets user data

        Keyword Arguments:
        username -- MyAnimeList username
        request -- type of data to get (profile, history, friends, animelist, mangalist)
        argument -- data for history (anime, manga), page number for friends, type of list
        page -- page number for animelist and mangalist
        """
        raise NotImplementedError

    @abstractmethod
    def meta(
        self,
        request: str,
        type: Optional[str],
        period: Optional[str],
        offset: Optional[int],
    ) -> Dict:
        """
        Gets meta information regarding the Jikan REST API

        Keyword Arguments:
        request -- requests (requests, status)
        type -- type to get info on, possible values in docs
        period -- time period (today, weekly, monthly)
        """
        raise NotImplementedError


from copy import deepcopy
from heapq import heappush, heappop, heapify
from graphAlgos.graphClass import GraphSearchBase
from constants import *

class DIJKSTRAS(GraphSearchBase):
    '''

    '''
    def __init__(self, start, target, pickup, grid, weights):
        super().__init__(start, target, pickup, grid, weights)
        self.pq = []
        heappush(self.pq, (0, start))

    def add_neighbors(self, row, col, curr_weight):
        for dir in self.directions:
            if self.valid_to_visit(row+dir[0], col+dir[1]):
                el_weight = self.weights[row+dir[0]][col+dir[1]] +\
                            curr_weight
                heappush(self.pq, (el_weight, (row+dir[0], col+dir[1])))
                self.visited_set.add((row+dir[0], col+dir[1]))
                self.parents[row+dir[0]][col+dir[1]] = (row, col)

    def finish(self):
        while self.pq:
            curr_weight, (curr_row, curr_col) = heappop(self.pq)
            self.curr_node = (curr_row, curr_col)
            self.order_visited.append(self.curr_node)

            self.add_neighbors(curr_row, curr_col, curr_weight)

    # Returns (found, alg_done, curr_spath_node, n_node_dir)
    def pickup_step(self):
        self.finding_shortest_path = True
        if self.pq:
            curr_weight, (curr_row, curr_col) = self.pq[0]
            self.shortest_path_length = curr_weight
            self.curr_node = (curr_row, curr_col)
            self.grid_updates()

            if (curr_row, curr_col) == self.pickup:
                self.pq = []
                heappush(self.pq, (0, self.pickup))
                self.pickup_done()
                return

            heappop(self.pq)
            self.pickup_order_visited.append(self.curr_node)
            self.add_neighbors(curr_row, curr_col, curr_weight)

    # Returns (found, alg_done, curr_spath_node, n_node_dir)
    def target_step(self):
        if self.drawing_shortest_path:
            self.step_through_shortest_path()
            return

        self.finding_shortest_path = True
        if self.pq:
            curr_weight, (curr_row, curr_col) = self.pq[0]
            self.shortest_path_length = curr_weight
            self.curr_node = (curr_row, curr_col)
            self.grid_updates()
            if (curr_row, curr_col) == self.target:
                self.done()
                return

            heappop(self.pq)
            self.order_visited.append(self.curr_node)
            self.add_neighbors(curr_row, curr_col, curr_weight)
            self.finding_shortest_path = True
        else:
            self.finding_shortest_path = False
            self.drawing_shortest_path = False


from selenium import webdriver
from base import *
import unittest

class IssueCreator(unittest.TestCase):

    def __init__(self):
        self.driver = webdriver.Firefox()
        self.config = configparser.ConfigParser()
        self.config.read("config.ini")

    def create_issue(self):
        self.driver.get(self.config.get('ISSUES', 'URL'))
        login = LoginPage(self.driver)
        # print("Opened Login Page")
        login.fill_sign_in()
        # print("FIlled the credentials")
        # print("Logged in")
        ci = IssuesPage(self.driver)
        ci.create_issue()
        # print("Issue Created")
        #close browser
        ci.close()
        # print("Browser Closed")

if __name__ == "__main__":
    ic = IssueCreator()
    ic.create_issue()


from django.urls import path,include
from django.contrib.auth import views as auth_views
from accounts.views import (SignUp,
                            LogIn,
                            LogOut,
                            MembersViewList,
                            UserDetail,
                            AddUser,
                            InviteMember
                           )

urlpatterns = [

    path('signup/', SignUp.as_view(), name='signup'),
    path('login/', LogIn.as_view(), name='login'),
    path('logout/', LogOut.as_view(), name='logout'),
    path('board/<int:board_id>/', MembersViewList.as_view(), name='invite'),
    path('user/<int:user_id>/', UserDetail.as_view(), name='userdetail'),
    path('user/<int:user_id>/added/',InviteMember.as_view(), name='memberadd'),

    path('', include('django.contrib.auth.urls')),
]


class Scale:
    def check(self):
        if self.count_person > 500:
            return f"{self.name}是个大公司"
        else:
            return f"{self.name}是个小公司"

class Detail:
    def show(self, scale):
        print(f'{scale},公司有{self.count_person}名员工。')

class Company(Detail, Scale):
    def __init__(self, name, count):
        self.name = name
        self.count_person = count

if __name__ == '__main__':
    my_company = Company("Fuck You", 800)
    company_scale = my_company.check()
    my_company.show(company_scale)


姓名 = ''
是不是傻逼 = ''
循环开关 = True

人物表 = {}

计数器 = 1

while 循环开关:
    姓名 = input('你条傻嗨叫咩名：')
    是不是傻逼 = input('你系唔系傻嗨？（系/唔系）')
    while 是不是傻逼 != '系' and 是不是傻逼 != '唔系':
        是不是傻逼 = input('系定唔系：')

    人物表[姓名] = 是不是傻逼

    是不是傻逼 = ''

    print('\n')

    if len(人物表.items()) >= 5:
        循环开关 = False

print('--------------------------------------------------------------------')
print('\n游戏已经结束啦，全部拉嗮返青山。')

print('下面系青山噶房号安排：\n')

for i,j in 人物表.items():
    print(f'{i}{j}傻嗨，安排系{计数器}号房。\n')
    计数器 += 1

print('--------------------------------------------------------------------\n')

input('分配完毕！\n输入任意内容退出或者直接关闭程序。\n')


import sqlite3

# Connect to the database
connection = sqlite3.connect("college_database.db")

# Create a cursor
cursor = connection.cursor()

sql_command = """INSERT INTO students (student_number,student_name, student_age, student_grade)
    VALUES (1,"Alan Heslop", 15, 5),
            (2,"Kieran Clayton", 19, 1),
           (3,"Rachael Atkinson", 19, 1);"""

# Execute your command
cursor.execute(sql_command)

# Commit
connection.commit()


def files_list(list_file):
	urls = []
	htmllist = open(list_file, 'r')
	for line in htmllist:
		line = line.strip()

		if line and (not line.startswith('#')):
			print("added: %s" % line)
			urls.append(line)
	htmllist.close()
	return urls


# note: this needs stem to run. you can
# install it in a fresh virtualenv on Debian (after "apt-get install
# python-virtualenv") like so:
#
# virtualenv venv
# source venv/bin/activate
# pip install --upgrade pip
# pip install stem
#
# Then you can run this:
# python3 poc.py
#
# ...and then visit any .onion address, and it'll get re-directed to
# txtorcon documentation.

import os
import subprocess
import sys
import time
import itertools
from copy import deepcopy
from os.path import join, split

from threading import Thread

import stem
from stem.control import EventType, Controller

from settings_services import _service_to_command
from settings_port import tor_control_port

class NameLookupError(Exception):
    def __init__(self, status):
        self.status = status
        msg = {
            0: 'The name resolution was successful',
            1: 'Name resolution generic failure',
            2: 'Name tld not recognized',
            3: 'Name not registered',
            4: 'Name resolution timeout exceeded',
        }
        super(NameLookupError, self).__init__(msg[status])

class _TorNameServiceProtocol(object):
    delimiter = '\n'

    def __init__(self, tor, process):
        self._queries = dict()
        self._id_gen = itertools.count(1)
        self._tor = tor
        self._process = process

    def watch_stdout(self):
        for line in self._process.stdout:
            self.lineReceived(line)

    def lineReceived(self, line):
        args = line.split()
        if args[0] == 'RESOLVED':
            # Answer might contain whitespace if it's an error message; if so,
            # len(answer) will be greater than 1.
            query_id, status, answer = args[1], args[2], args[3:]
            query_id = int(query_id)
            status = int(status)

            try:
                stream_id = self._queries[query_id]
                del self._queries[query_id]
            except KeyError:
                print("No query {}: {}".format(query_id, self._queries.keys()))

            if status == 0:
                # Answer should be a domain name or IP address, neither of
                # which will contain whitespace, so only take the first
                # whitespace-separated token.
                answer = answer[0]
                self._tor.msg('REDIRECTSTREAM ' + stream_id + ' ' + answer)
                try:
                    self._tor.attach_stream(stream_id, 0)
                except stem.UnsatisfiableRequest:
                    pass
            else:
                self._tor.close_stream(stream_id, stem.RelayEndReason.RESOLVEFAILED)

    def request_lookup(self, stream_id, name):
        query_id = next(self._id_gen)
        self._queries[query_id] = stream_id
        self._process.stdin.write('RESOLVE {} {}\n'.format(query_id, name))

def spawn_name_service(tor, name):
    try:
        args = _service_to_command[name]
    except KeyError:
        raise Exception(
            "No such service '{}'".format(name)
        )
    spawn_env = deepcopy(os.environ)
    spawn_env.update({
        'TOR_NS_STATE_LOCATION': '/var/lib/tor/ns_state',
        'TOR_NS_PROTO_VERSION': '1',
        'TOR_NS_PLUGIN_OPTIONS': '',
    })
    process = subprocess.Popen(args, bufsize=1, stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                               universal_newlines=True, env=spawn_env)

    proto = _TorNameServiceProtocol(tor, process)

    t = Thread(target=proto.watch_stdout)
    t.start()

    return proto

class _Attacher(object):
    def __init__(self, tor):
        self._tor = tor
        self._services = {}

    def maybe_launch_service(self, name):
        suffix = None
        srv = None

        for candidate_suffix in _service_to_command:
            if name.endswith("." + candidate_suffix):
                suffix = candidate_suffix
                srv = self._services.get(suffix, None)
                break

        if srv is None:
            srv = spawn_name_service(self._tor, suffix)
            self._services[suffix] = srv
        return srv

    def attach_stream(self, stream):
        print("attach_stream {}".format(stream))

        # Not all stream events need to be attached.
        # TODO: check with Tor Project whether NEW and NEWRESOLVE are the correct list.
        if stream.status not in [stem.StreamStatus.NEW, stem.StreamStatus.NEWRESOLVE]:
            return

        try:
            srv = self.maybe_launch_service(stream.target_address)
        except Exception:
            print("Unable to launch service for '{}'".format(stream.target_address))
            try:
                self._tor.attach_stream(stream.id, 0)
            except stem.UnsatisfiableRequest:
                pass
            return

        srv.request_lookup(stream.id, stream.target_address)

def main():
    while True:
        try:
            # open main controller
            controller = Controller.from_port(port = tor_control_port)
            break
        except stem.SocketError:
            time.sleep(0.005)

    controller.authenticate()

    print("[notice] Successfully connected to the Tor control port.")

    if controller.get_conf('__LeaveStreamsUnattached') != '1':
        sys.exit('[err] torrc is unsafe for name lookups.  Try adding the line "__LeaveStreamsUnattached 1" to torrc-defaults')

    attacher = _Attacher(controller)

    controller.add_event_listener(attacher.attach_stream, EventType.STREAM)

    print('[debug] Now monitoring stream connections.')

    try:
        # Sleeping for 365 days, as upstream OnioNS does, appears to be incompatible with Windows.
        # Therefore, we instead sleep for 1 day inside an infinite loop.
        while True:
            time.sleep(60 * 60 * 24 * 1) #basically, wait indefinitely
    except KeyboardInterrupt:
        print('')

if __name__ == '__main__':
  main()


# Py program to take a list and prints the largest number in the list.

# declare an empty list
a_list = []

num = int(input("\nEnter the number of elements : "))

for i in range(1, (num + 1)):
    var = int(input("\nEnter an element : "))
    # appending\adding to the list
    a_list.append(var)

print(f"\nList entered is : {a_list}")

# sorting the list in ascending order
a_list.sort()

print(f"\nSorted list is : {a_list}")

# print the last element of the list, which will be the largest after sorting
print("\nLargest element in the list : ", a_list[num - 1])


#from xlrd import open_workbook
from datetime import datetime
import pandas as pd
import openpyxl  # necessary for pandas ExcelWriter
import pyodbc
import csv
import os
import sys
import time
import yaml

#for SB375
#SB375 CO2
#SB375 VMT
####for SB375 Emission Factor
#SB375 CO2 RunEx
#SB375 CO2 StartEx
#SB 375 vehicle trips

# Start Script Timer
start = time.time()

def find(name, path):
	for root, dirs, files in os.walk(path):
		for file in files:
			if name in file:
				return os.path.join(root, file)

usage = ("Correct Usage: process_aq_sb375.py <EMFAC version: 2014 | 2017> "
        "<Output Folder> <scenario_id1> <scenario_id2> <scenario_id3> ...")
# user inputs
# raise error if emfac version argument passed incorrectly
emfac_version = sys.argv[1]
if emfac_version not in ["2014", "2017"]:
    print(usage)
    sys.exit(-1)

output_folder = sys.argv[2]

# get SQL server attributes from local file
with open("../../resources/emfac/database-specs.yaml", "r", encoding="utf8") as stream:
    try:
        db_info = yaml.safe_load(stream)
    except yaml.YAMLError as exc:
        print(exc)

# set sql server connection string
# noinspection PyArgumentList
sql_con = pyodbc.connect(driver='{SQL Server}',
                         server=db_info['server1']['name'],
                         database=db_info['server1']['db1_1'],
                         trusted_connection='yes')

#base_folder = 'T:\\rtp\\2019rp\\rp19_scen\\abm_runs_bod\\'
#output_folder = '\\output'
#folder = 'T:\\RTP\\2021RP\\2021rp_draft_v1\\abm_runs\\emfac\\emfac2014\\'

with open(output_folder+'/summary_emfac'+ str(emfac_version)+'_SB375_' + str(datetime.now().strftime("%Y-%m-%d")+'.csv'), 'w', newline='') as csvfile:
	writer = csv.writer(csvfile)
	writer.writerow(['Scenario','Scenario_ID','SB375_CO2_runex','SB375_VMT','SB375_CO2_startEx','SB375_vehicle_trip','SB375_CO2'])
					 #scenario,id, sb375_VMT, sb375_CO2,SB375_co2_runex,SB375_co2_runEx,SB375_vehicle_trip])
	for id_argv in sys.argv[3:]:
		scenario_id = int(id_argv)
		scenario = pd.read_sql_query(
			sql=("SELECT RTRIM([name]) AS [name], [year], RTRIM(path) as path  "
			 "FROM [dimension].[scenario] WHERE [scenario_id] = ?"),
			con=sql_con,
			params=[scenario_id]
		)
		#folder = scenario.at[0, "path"] + '\\output'

		file_name_pattern = 'EMFAC'+ str(emfac_version) +'-SANDAG-' + scenario.at[0, "name"] + '-' + str(scenario_id) + '-Annual-' + str(scenario.at[0, "year"]) + '-sb375_sb375'
		xlsx = find(file_name_pattern, output_folder)

		if xlsx:
			wb = openpyxl.load_workbook(xlsx)
			sheet = wb['Total SANDAG']
			sb375_vmt = sheet.cell(2,11).value
			sb375_co2 = sheet.cell(2,16).value
			sb375_vehicle_trip = sheet.cell(2,12).value
			sb375_co2_runex = sheet.cell(2,13).value
			sb375_co2_startex = sheet.cell(2,15).value

			writer.writerow([scenario.at[0, "name"], scenario_id, \
							sb375_co2_runex,sb375_vmt, sb375_co2_startex,sb375_vehicle_trip,sb375_co2])
		else:
			print(f"\nFAILED TO OPEN: {file_name_pattern}.\n If this is unexpected behavior, please re-verify if file exists.\n")

	print(f"\n FILE CREATED AT: {csvfile.name}\n")


import boto3, importlib, json

from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder

from nc import tasks as nc_tasks
from nc.queues.backends.base import BaseQueueBackend

class SQSBoto3QueueBackend(BaseQueueBackend):
    """
    QueueBackend implementation for SQS Boto3 (daemon: aws-sqsd).

    Needs celery modules to work since using celery.app.task.
    """
    def __init__(self):
        self.client = boto3.client(
            'sqs',
            region_name=settings.AWS_SQS_REGION_NAME,
            aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
            aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
        )
        self.queue_url = settings.AWS_SQS_BROKER_URL

    def delay(self, task, *args, **kwargs):
        """
        POST to SQS queue the appropriate JSON parameters
        for worker to eventually consume task.
        """
        response = self._send_message(task, *args, **kwargs)

    def _send_message(self, task, *args, **kwargs):
        """
        Send message to queue with task and input params.

        Return response of Successful and Failed messages.
        """
        return self.client.send_message(
            QueueUrl=self.queue_url,
            MessageBody=self._format_message_body(task, *args, **kwargs),
        )

    def _format_message_body(self, task, *args, **kwargs):
        """
        Format message body for MessageBody of SQS queue message.
        """
        return json.dumps({
            'task': task.__name__,
            'args': args,
            'kwargs': kwargs,
        }, cls=DjangoJSONEncoder)

    def process(self, message):
        """
        Process message in worker tier.
        """
        message = json.loads(message)
        task = getattr(nc_tasks, message.get('task'), None)
        if task:
            args = message.get('args', [])
            kwargs = message.get('kwargs', {})
            task(*args, **kwargs)


import discord
from discord.ext import commands
import json
import random
import os

#開啟檔案
with open('setting.json', 'r', encoding='utf8') as jFile:
    jdata = json.load(jFile)

#機器人指令開頭
bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    print('機器人測試上線')
    channel = bot.get_channel(int(jdata['test_channel']))
    await channel.send('機器人已上線  :100:')

@bot.command()
async def close(ctx):
    print('機器人測試離線')
    await ctx.send('機器人已離線  :skull:')
    await bot.close()

@bot.command()
async def load(ctx, extension):
    bot.load_extension(F'cmds.{extension}')
    await ctx.send(F'載入 {extension} 完畢')

@bot.command()
async def unload(ctx, extension):
    bot.unload_extension(F'cmds.{extension}')
    await ctx.send(F'卸載 {extension} 完畢')

@bot.command()
async def reload(ctx, extension):
    bot.reload_extension(F'cmds.{extension}')
    await ctx.send(F'重載 {extension} 完畢')

for Filename in os.listdir('./cmds'):
    if Filename.endswith('.py'):
        bot.load_extension(F'cmds.{Filename[:-3]}')

if __name__ == "__main__":
    bot.run(jdata['TOKEN'])


import os
import pandas as pd
from getpass import getuser
import matplotlib.pyplot as plt

FOLDER = 'lunarlander'

 # Folder name for good ol' Windows
dirname = os.path.dirname(__file__)
out_folder = os.path.join(dirname, '../models/'+FOLDER+'/')
username = getuser()
filename = out_folder + username + '-' + 'logs.csv'
plot_folder=os.path.join(dirname,'../plots')

df = pd.read_csv(filename, header=0, names=['Date', 'Generation', 'Mean', 'Min', 'Max'])

ax = plt.gca()
df.plot(kind='line', x='Generation', y='Mean', ax=ax)
df.plot(kind='line', x='Generation', y='Min', ax=ax)
df.plot(kind='line', x='Generation', y='Max', ax=ax)
plt.ylabel('Fitness')
plt.title(FOLDER.capitalize()+' environment')

if not os.path.exists(plot_folder):
    os.mkdir(plot_folder)

plt.savefig(plot_folder +'/'+FOLDER+'.png')


# -*- coding: utf-8 -*-
"""
Created on Thu Sep  1 19:34:47 2016

@author: Wuxiaoshen
"""

def singleNumber(A):
    one, two = 0, 0
    for x in A:
        one, two = (~x & one) | (x & ~one & ~two), (~x & two) | (x & one)
    return one

A = singleNumber([1,2,2,2,1,1,3])
print(A)

def threeofone(numbers):
    bit=[0]*32
    for i in numbers:
        for j in range(32):
            bit[j]+=(i>>j)&1
    print(bit)
    result = 0
    for k in range(32):
        if bit[k]%4!=0:
            result+=(1<<k)
    return result
A = threeofone([1,1,1,1,3,4,4,4,4])
print(A)


import socket
import sys
import time
import os

G_HOST = '127.0.0.1'
G_PORT = 3333
G_DATA = os.urandom(1000)
G_TOTAL_SENDS = 100

def log(message):
    print('[sender] {}'.format(message))

class Sender:
    def __init__(self, da_host, da_port):
        self.da_host = da_host
        self.da_port = da_port

        self.da_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.da_socket.connect((self.da_host, self.da_port))

    def start(self):
        for i in range(G_TOTAL_SENDS):
            # send data
            self.da_socket.send(G_DATA)
            time.sleep(0.01)

    def stop(self):
        self.da_socket.close()

a = Sender(G_HOST, G_PORT)
a.start()
a.stop()