import unittest
import sys

from robot.variables import variables, is_list_var, is_scalar_var, is_var
from robot.errors import *
from robot import utils
from robot.utils.asserts import *

SCALARS = [ '${var}', '${  v A  R }' ]
LISTS = [ '@{var}', '@{  v A  R }' ]
NOKS = [ 'var', '$var', '${var', '${va}r', '@{va}r', '@var', '%{var}',
         ' ${var}', '@{var} ', '\\${var}', '\\\\${var}' ]

# Simple objects needed when testing assigning objects to variables.
# JavaObject lives in '../../acceptance/testdata/libraries'

class PythonObject:
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def __str__(self):
        return '(%s, %s)' % (self.a, self.b)
    __repr__ = __str__

if utils.is_jython:
    import JavaObject

class TestIsMethods(unittest.TestCase):

    def test_is_var(self):
        for ok in SCALARS + LISTS:
            assert is_var(ok)
        for nok in NOKS:
            assert not is_var(nok)

    def test_is_scalar_var(self):
        for ok in SCALARS:
            assert is_scalar_var(ok)
        for nok in LISTS + NOKS:
            assert not is_scalar_var(nok)

    def test_is_list_var(self):
        for ok in LISTS:
            assert is_list_var(ok)
        for nok in SCALARS + NOKS:
            assert not is_list_var(nok)

class TestVariables(unittest.TestCase):

    def setUp(self):
        self.varz = variables.Variables()

    def test_set(self):
        for var in SCALARS + LISTS:
            assert not self.varz.has_key(var)
            self.varz[var] = ['value']
            assert self.varz.has_key(var), var
            assert self.varz.has_key(var.lower().replace(' ',''))
            self.varz.clear()

    def test_set_invalid(self):
        for var in NOKS:
            try:
                self.varz[var] = ['value']
            except DataError:
                pass
            else:
                fail()

    def test_set_scalar(self):
        for var in SCALARS:
            for value in [ 'str', 10, ['hi','u'], ['hi',2], {'a':1,'b':2}, self,
                           None, unittest.TestCase ]:
                self.varz[var] = value
                act = self.varz[var]
                assert act == value, '%s: %s %s != %s %s' \
                        % (var, act, type(act), value, type(value))
                self.varz.clear()
        self.varz['${myvar}'] = ''
        assert_equals(self.varz['${myvar}'], '')

    def test_update(self):
        self.varz['${a}'] = 1
        self.varz.update({'${b}':2})
        for k, v in [('${a}', 1), ('${b}', 2)]:
            assert_true(k in self.varz)
            assert_true(k in self.varz.keys())
            assert_equals(self.varz[k], v)

    def test_update_invalid(self):
        self.varz['${a}'] = 1
        assert_raises(DataError, self.varz.update, {'invalid variable name':2})

    def test_set_list(self):
        for var in LISTS:
            for value in [ [], [''], ['str'], [10], ['hi','u'], ['hi',2],
                           [{'a':1,'b':2}, self, None] ]:
                self.varz[var] = value
                assert_equals(self.varz[var], value)
                self.varz.clear()

    def test_getitem_invalid(self):
        for var in NOKS:
            self.assertRaises(DataError, self.varz.__getitem__, var)

    def test_has_key(self):
        self.varz['${k}'] = 'v'
        assert self.varz.has_key('${k}')
        assert self.varz.has_key('${1}')
        assert self.varz.has_key('${k.upper()}')
        assert not self.varz.has_key('${non-existing}')

    def test_contains(self):
        self.varz['${k}'] = 'v'
        assert '${k}' in self.varz
        assert '${-3}' in self.varz
        assert '${k.upper()}' in self.varz
        assert '${nok}' not in self.varz

    def test_replace_scalar(self):
        self.varz['${foo}'] = 'bar'
        self.varz['${a}'] = 'ari'
        for inp, exp in [ ('${foo}','bar'), ('${a}','ari'),
                          ('${a','${a'), ('',''), ('hii','hii'),
                          ("Let's go to ${foo}!", "Let's go to bar!"),
                          ('${foo}ba${a}-${a}', 'barbaari-ari') ]:
            assert_equals(self.varz.replace_scalar(inp), exp)

    def test_replace_list(self):
        self.varz['@{L}'] = ['v1','v2']
        self.varz['@{E}'] = []
        self.varz['@{S}'] = ['1','2','3']
        for inp, exp in [ (['@{L}'], ['v1','v2']),
                          (['@{L}','v3'], ['v1','v2','v3']),
                          (['v0','@{L}','@{E}','v@{S}[2]'], ['v0','v1','v2','v3']),
                          ([], []), (['hi u','hi 2',3], ['hi u','hi 2',3]) ]:
            assert_equals(self.varz.replace_list(inp), exp)

    def test_replace_list_in_scalar_context(self):
        self.varz['@{list}'] = ['v1','v2']
        assert_equals(self.varz.replace_list(['@{list}']), ['v1', 'v2'])
        assert_equals(self.varz.replace_list(['-@{list}-']), ["-['v1', 'v2']-"])

    def test_replace_list_item(self):
        self.varz['@{L}'] = ['v0','v1']
        assert_equal(self.varz.replace_list(['@{L}[0]']), ['v0'])
        assert_equal(self.varz.replace_scalar('@{L}[1]'), 'v1')
        assert_equal(self.varz.replace_scalar('-@{L}[0]@{L}[1]@{L}[0]-'), '-v0v1v0-')
        self.varz['@{L2}'] = ['v0',['v11','v12']]
        assert_equal(self.varz.replace_list(['@{L2}[0]']), ['v0'])
        assert_equal(self.varz.replace_list(['@{L2}[1]']), [['v11','v12']])
        assert_equal(self.varz.replace_scalar('@{L2}[0]'), 'v0')
        assert_equal(self.varz.replace_scalar('@{L2}[1]'), ['v11','v12'])
        assert_equal(self.varz.replace_list(['@{L}[0]','@{L2}[1]']), ['v0',['v11','v12']])

    def test_replace_non_strings(self):
        self.varz['${d}'] = {'a':1,'b':2}
        self.varz['${n}'] = None
        assert_equal(self.varz.replace_scalar('${d}'), {'a':1,'b':2})
        assert_equal(self.varz.replace_scalar('${n}'), None)

    def test_replace_non_strings_inside_string(self):
        class Example:
            def __str__(self):
                return 'Hello'
        self.varz['${h}'] = Example()
        self.varz['${w}'] = 'world'
        res = self.varz.replace_scalar('Another "${h} ${w}" example')
        assert_equals(res, 'Another "Hello world" example')

    def test_replace_list_item_invalid(self):
        self.varz['@{L}'] = ['v0','v1','v3']
        for inv in [ '@{L}[3]', '@{NON}[0]', '@{L[2]}' ]:
            self.assertRaises(DataError, self.varz.replace_list, [inv])

    def test_replace_non_existing_list(self):
        self.assertRaises(DataError, self.varz.replace_list, ['${nonexisting}'])

    def test_replace_non_existing_scalar(self):
        self.assertRaises(DataError, self.varz.replace_scalar, '${nonexisting}')

    def test_replace_non_existing_string(self):
        self.assertRaises(DataError, self.varz.replace_string, '${nonexisting}')

    def test_replace_escaped(self):
        self.varz['${foo}'] = 'bar'
        for inp, exp in [ (r'\${foo}', r'${foo}'),
                          (r'\\${foo}', r'\bar'),
                          (r'\\\${foo}', r'\${foo}'),
                          (r'\\\\${foo}', r'\\bar'),
                          (r'\\\\\${foo}', r'\\${foo}') ]:
            assert_equals(self.varz.replace_scalar(inp), exp)

    def test_variables_in_value(self):
        self.varz['${exists}'] = 'Variable exists but is still not replaced'
        self.varz['${test}'] = '${exists} & ${does_not_exist}'
        assert_equals(self.varz['${test}'], '${exists} & ${does_not_exist}')
        self.varz['@{test}'] = ['${exists}', '&', '${does_not_exist}']
        assert_equals(self.varz['@{test}'], '${exists} & ${does_not_exist}'.split())

    def test_variable_as_object(self):
        obj = PythonObject('a', 1)
        self.varz['${obj}'] = obj
        assert_equals(self.varz['${obj}'], obj)
        expected = ['Some text here %s and %s there' % (obj,obj)]
        actual = self.varz.replace_list(['Some text here ${obj} and ${obj} there'])
        assert_equals(actual, expected)

    def test_extended_variables(self):
        # Extended variables are vars like ${obj.name} when we have var ${obj}
        obj = PythonObject('a', [1,2,3])
        dic = { 'a': 1, 'o': obj }
        self.varz['${obj}'] = obj
        self.varz['${dic}'] = dic
        assert_equals(self.varz.replace_scalar('${obj.a}'), 'a')
        assert_equals(self.varz.replace_scalar('${obj.b}'), [1,2,3])
        assert_equals(self.varz.replace_scalar('${obj.b[0]}-${obj.b[1]}'), '1-2')
        assert_equals(self.varz.replace_scalar('${dic["a"]}'), 1)
        assert_equals(self.varz.replace_scalar('${dic["o"]}'), obj)
        assert_equals(self.varz.replace_scalar('-${dic["o"].b[2]}-'), '-3-')

    def test_space_is_not_ignored_after_newline_in_extend_variable_syntax(self):
        self.varz['${x}'] = 'test string'
        self.varz['${lf}'] = '\\n'
        self.varz['${lfs}'] = '\\n '
        for inp, exp in [('${x.replace(" ", """\\n""")}', 'test\nstring'),
                         ('${x.replace(" ", """\\n """)}', 'test\n string'),
                         ('${x.replace(" ", """${lf}""")}', 'test\nstring'),
                         ('${x.replace(" ", """${lfs}""")}', 'test\n string')]:
            assert_equals(self.varz.replace_scalar(inp), exp)

    def test_escaping_with_extended_variable_syntax(self):
        self.varz['${p}'] = 'c:\\temp'
        assert self.varz['${p}'] == 'c:\\temp'
        assert_equals(self.varz.replace_scalar('${p + "\\\\foo.txt"}'),
                      'c:\\temp\\foo.txt')

    def test_internal_variables(self):
        # Internal variables are variables like ${my${name}}
        self.varz['${name}'] = 'name'
        self.varz['${my name}'] = 'value'
        assert_equals(self.varz.replace_scalar('${my${name}}'), 'value')
        self.varz['${whos name}'] = 'my'
        assert_equals(self.varz.replace_scalar('${${whos name} ${name}}'), 'value')
        assert_equals(self.varz.replace_scalar('${${whos${name}}${name}}'), 'value')
        self.varz['${my name}'] = [1,2,3]
        assert_equals(self.varz.replace_scalar('${${whos${name}}${name}}'), [1,2,3])
        assert_equals(self.varz.replace_scalar('- ${${whos${name}}${name}} -'), '- [1, 2, 3] -')

    def test_math_with_internal_vars(self):
        assert_equals(self.varz.replace_scalar('${${1}+${2}}'), 3)
        assert_equals(self.varz.replace_scalar('${${1}-${2}}'), -1)
        assert_equals(self.varz.replace_scalar('${${1}*${2}}'), 2)
        assert_equals(self.varz.replace_scalar('${${1}/${2}}'), 0)

    def test_math_with_internal_vars_with_spaces(self):
        assert_equals(self.varz.replace_scalar('${${1} + ${2.5}}'), 3.5)
        assert_equals(self.varz.replace_scalar('${${1} - ${2} + 1}'), 0)
        assert_equals(self.varz.replace_scalar('${${1} * ${2} - 1}'), 1)
        assert_equals(self.varz.replace_scalar('${${1} / ${2.0}}'), 0.5)

    def test_math_with_internal_vars_does_not_work_if_first_var_is_float(self):
        assert_raises(DataError, self.varz.replace_scalar, '${${1.1}+${2}}')
        assert_raises(DataError, self.varz.replace_scalar, '${${1.1} - ${2}}')
        assert_raises(DataError, self.varz.replace_scalar, '${${1.1} * ${2}}')
        assert_raises(DataError, self.varz.replace_scalar, '${${1.1}/${2}}')

    def test_list_variable_as_scalar(self):
        self.varz['@{name}'] = exp = ['spam', 'eggs']
        assert_equals(self.varz.replace_scalar('${name}'), exp)
        assert_equals(self.varz.replace_list(['${name}', 42]), [exp, 42])
        assert_equals(self.varz.replace_string('${name}'), str(exp))
        assert_true(self.varz.has_key('${name}'))

    def test_copy(self):
        varz = variables.Variables(identifiers=['$'])
        varz['${foo}'] = 'bar'
        copy = varz.copy()
        assert_equals(copy['${foo}'], 'bar')
        assert_equals(copy._identifiers, ['$'])

    if utils.is_jython:

        def test_variable_as_object_in_java(self):
            obj = JavaObject('hello')
            self.varz['${obj}'] = obj
            assert_equals(self.varz['${obj}'], obj)
            assert_equals(self.varz.replace_scalar('${obj} world'), 'hello world')

        def test_extended_variables_in_java(self):
            obj = JavaObject('my name')
            self.varz['${obj}'] = obj
            assert_equals(self.varz.replace_list(['${obj.name}']), ['my name'])

class TestVariableSplitter(unittest.TestCase):

    _identifiers = ['$','@','%','&','*']

    def test_empty(self):
        self._test('', None)

    def test_no_vars(self):
        for inp in ['hello world', '$hello', '{hello}', '$\\{hello}',
                    '${hello', '$hello}' ]:
            self._test(inp, None)

    def test_backslashes(self):
        for inp in ['\\', '\\\\', '\\\\\\\\\\',
                    '\\hello\\\\world\\\\\\']:
            self._test(inp, None)

    def test_one_var(self):
        self._test('${hello}', '${hello}', 0)
        self._test('1 @{hello} more', '@{hello}', 2)
        self._test('*{hi}}', '*{hi}', 0)
        self._test('{%{{hi}}', '%{{hi}', 1)
        self._test('-= ${} =-', '${}', 3)
        # In this case splitter thinks there are internal but there aren't.
        # Better check would probably spent more time than that is saved when
        # variable base is processed again in this special case.
        self._test('%{hi%{u}', '%{hi%{u}', 0, internal=True)

    def test_multiple_vars(self):
        self._test('${hello} ${world}', '${hello}', 0)
        self._test('hi %{u}2 and @{u2} and also *{us3}', '%{u}', 3)
        self._test('0123456789 %{1} and @{2', '%{1}', 11)

    def test_escaped_var(self):
        self._test('\\${hello}', None)
        self._test('hi \\\\\\${hello} moi', None)

    def test_not_escaped_var(self):
        self._test('\\\\${hello}', '${hello}', 2)
        self._test('\\hi \\\\\\\\\\\\${hello} moi', '${hello}',
                   len('\\hi \\\\\\\\\\\\'))
        self._test('\\ ${hello}', '${hello}', 2)
        self._test('${hello}\\', '${hello}', 0)
        self._test('\\ \\ ${hel\\lo}\\', '${hel\\lo}', 4)

    def test_escaped_and_not_escaped_vars(self):
        for inp, var, start in [
                ('\\${esc} ${not}', '${not}', len('\\${esc} ')),
                ('\\\\\\${esc} \\\\${not}', '${not}',
                 len('\\\\\\${esc} \\\\')),
                ('\\${esc}\\\\${not}${n2}', '${not}', len('\\${esc}\\\\')) ]:
            self._test(inp, var, start)

    def test_internal_vars(self):
        for inp, var, start in [
                ('${hello${hi}}', '${hello${hi}}', 0),
                ('bef ${${hi}hello} aft', '${${hi}hello}', 4),
                ('\\${not} ${hel${hi}lo} ', '${hel${hi}lo}', len('\\${not} ')),
                ('${${hi}${hi}}\\', '${${hi}${hi}}', 0),
                ('${${hi${hi}}} ${xx}', '${${hi${hi}}}', 0),
                ('${xx} ${${hi${hi}}}', '${xx}', 0),
                ('${\\${hi${hi}}}', '${\\${hi${hi}}}', 0),
                ('\\${${hi${hi}}}', '${hi${hi}}', len('\\${')),
                ('\\${\\${hi\\\\${hi}}}', '${hi}', len('\\${\\${hi\\\\')) ]:
            internal = var.count('{') > 1
            self._test(inp, var, start, internal=internal)

    def test_index(self):
        self._test('@{x}[0]', '@{x}', 0, '0')
        self._test('.@{x}[42]..', '@{x}', 1, '42')
        self._test('@{x}[${i}] ${xyz}', '@{x}', 0, '${i}')
        self._test('@{x}[]', '@{x}', 0, '')
        self._test('@{x}[inv]', '@{x}', 0, 'inv')
        self._test('@{x}[0', '@{x}', 0, None)
        self._test('@{x}}[0]', '@{x}', 0, None)
        self._test('${x}[0]', '${x}', 0, None)
        self._test('%{x}[0]', '%{x}', 0, None)
        self._test('*{x}[0]', '*{x}', 0, None)
        self._test('&{x}[0]', '&{x}', 0, None)

    def test_custom_identifiers(self):
        for inp, start in [ ('@{x}${y}', 4),
                            ('%{x} ${y}', 5),
                            ('*{x}567890${y}', 10),
                            ('&{x}%{x}@{x}\\${x}${y}',
                             len('&{x}%{x}@{x}\\${x}')) ]:
            self._test(inp, '${y}', start, identifiers=['$'])

    def test_identifier_as_variable_name(self):
        for i in self._identifiers:
            for count in 1,2,3,42:
                var = '%s{%s}' % (i, i*count)
                self._test(var, var)
                self._test(var+'spam', var)
                self._test('eggs'+var+'spam', var, start=4)
                self._test(i+var+i, var, start=1)

    def test_identifier_as_variable_name_with_internal_vars(self):
        for i in self._identifiers:
            for count in 1,2,3,42:
                var = '%s{%s{%s}}' % (i, i*count, i)
                self._test(var, var, internal=True)
                self._test('eggs'+var+'spam', var, start=4, internal=True)
                var = '%s{%s{%s}}' % (i, i*count, i*count)
                self._test(var, var, internal=True)
                self._test('eggs'+var+'spam', var, start=4, internal=True)

    def _test(self, inp, variable, start=0, index=None, identifiers=None,
              internal=False):
        if variable is not None:
            identifier = variable[0]
            base = variable[2:-1]
            end = start + len(variable)
            if index is not None:
                end += len(index) + 2
        else:
            identifier = base = None
            start = end = -1
        if not identifiers:
            identifiers = self._identifiers
        res = variables.VariableSplitter(inp, identifiers)
        assert_equals(res.base, base, "'%s' base" % inp)
        assert_equals(res.start, start, "'%s' start" % inp)
        assert_equals(res.end, end, "'%s' end" % inp)
        assert_equals(res.identifier, identifier, "'%s' indentifier" % inp)
        assert_equals(res.index, index, "'%s' index" % inp)
        assert_equals(res._may_have_internal_variables, internal,
                      "'%s' internal" % inp)

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


import keras
from keras.models import load_model
from keras import backend as k
import os
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import sys
import sklearn.metrics as metrics
import cv2
import sys
import re
from keras.utils import np_utils
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import csv
#to make tensorflow shut up
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

#targeted fast gradient sign method, which pushes a class towards a specific label
def single_targeted_FGSM(model, img, label, epsilon):
	'''
	model : keras Sequential model
			neural network object to get info from
	imgs : images of 224 by 224 by 3
		the input images that we are going to alter
	targets : array
			one-hot encoding vectors that contains the targeted labels of the classes
	'''
	target = get_target_label(label)
	y_target = keras.Input(shape=target.shape)
	#define loss function
	loss = k.mean(k.categorical_crossentropy(y_target, model.output))
	#get derivative/gradient of loss wrt input
	loss_gradient = k.gradients(loss, model.inputs)
	#build function
	func = k.function(model.inputs + [y_target], loss_gradient)
	#get gradient
	gradient = func([img, target])
	gradient = np.sign(gradient)
	print(gradient)
	gradient = gradient * epsilon
	print(gradient)
	adv_img = img - gradient
	return adv_img

def test_imgs(model, imgs, labels):
	'''
	model: keras Sequential model object
		   model with which to make predictions
	imgs: numpy array
		  vector of images to test
	labels: numpy array
			one-hot labels of images, should have same length as imgs
	'''
	accuracy = 0
	avg_confidence = 0

	for i in range(0, len(labels)):
		x = imgs[i]
		#check to see if image is 4 dimensional
		if len(x.shape) != 4:
			#if not, wrap it in one dimension
			x = np.expand_dims(x, axis=0)
		#this is for VGG16 network only
		x = preprocess_input(x)
		#get prediction of image
		prediction = model.predict(x)
		#get class label
		pred = np.argmax(prediction[0])
		#get truth
		actual = np.argmax(labels[i])
		#get confidence of prediction
		confidence = max(prediction[0])
		avg_confidence += confidence
		#check for accuracy
		if pred == actual:
			accuracy += 1
	return accuracy/len(labels), avg_confidence/len(labels)

#function to nicely display images in matplotlib
def display_image(model, x, x_adv, label, e):
	'''
	model : keras Sequential model object
			the neural network trained on the CINIC 10 images
	x : 224 by 224 by 3 numpy array
		the unaltered, original image
	x_adv : 224 by 224 by 3 numpy array
			the adversarial image
	label : numpy array
			one-hot encoded vector, true labels of the images
	e : int or float
		how much the image was altered, used here to output stats
	'''
	#actual labels of the images
	class_labels = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]

	#initialize plot
	f = plt.figure()
	plt.title("Class %s example" % class_labels[np.argmax(label)])
	plt.axis("off")

	#add original image
	f.add_subplot(1, 2, 1)
	plt.title("Original Image")
	#prediction and confidence
	p, c = prediction(model, x)
	#some preprocessing before sending image off to matplotlib
	x = np.squeeze(x)
	#needed to reverse channels from RGB to BGR so the colors didnt come out blue in matplotlib
	plt.imshow(x[...,::-1])
	#show annotation with prediction and confidence score
	plt.annotate("Original Image\nClass prediction: %s\nProbability: %.4f" % (class_labels[p], c), (0, 0), (0, -20), xycoords='axes fraction', textcoords='offset points', va='top')

	#add adversarial image
	f.add_subplot(1, 2, 2)
	plt.title("Adversarial Image, e=%d" % e)
	p, c = prediction(model, x_adv)
	#some preprocessing before sending image off to matplotlib
	x_adv = np.clip(x_adv, 0, 255)
	x_adv = np.squeeze(x_adv)
	x_adv /= 255
	plt.imshow(x_adv[...,::-1])
	#show annotation with prediction and confidence score
	plt.annotate("Adversarial Image\nClass prediction: %s\nProbability: %.4f" % (class_labels[p], c), (0, 0), (0, -20), xycoords='axes fraction', textcoords='offset points', va='top')
	plt.savefig("results/img_%d.png" % e, bbox_inches="tight")

#helper function to predict image class in img_test function
def prediction(model, image):
	'''
	model : keras Sequential model
			neural network object to prediction with
	img : image of 224 by 224 by 3
		the image to predict
	'''
    #reshape image for keras
	img = image.reshape(1, 224, 224, 3)
    #make prediction
	pred = model.predict(img)
	#flatten
	pred = list(pred[0])
	#get index of class label
	p = pred.index(max(pred))
	#get confidence score
	c = max(pred)
	#return prediction label and confidence score
	return p, c

#regular fast gradient sign method
def FGSM(model, imgs, labels):
	'''
	model : keras Sequential model
			neural network object to get info from
	imgs : image of 224 by 224 by 3
		the input image that we are going to alter
	labels : array
			one-hot encoding vector that contains the true label of the class
	'''
	#get shape of label to use later
	y_true = keras.Input(shape=labels[0].shape)
	#define loss function
	loss = k.mean(k.categorical_crossentropy(y_true, model.output))
	#get derivative/gradient of loss wrt input
	loss_gradient = k.gradients(loss, model.inputs)
	#build function
	func = k.function(model.inputs + [y_true], loss_gradient)
	#get gradients
	gradients = []
	start = 0
	end = int(len(labels)/20)
	increment = int(len(labels)/20)
	#loop to avoid OOM error
	for i in range(0, 20):
		tmp = func([np.squeeze(imgs[start:end]), labels[start:end]])[0]
		start = end
		end += increment
		gradients.append(tmp)
	gradients = np.array(gradients)
	gradients = np.reshape(gradients, (len(labels), 224, 224, 3))
	return gradients

#targeted fast gradient sign method, which pushes a class towards a specific label
def targeted_FGSM(model, imgs, targets):
	'''
	model : keras Sequential model
			neural network object to get info from
	imgs : images of 224 by 224 by 3
		the input images that we are going to alter
	targets : array
			one-hot encoding vectors that contains the targeted labels of the classes
	'''
	y_target = keras.Input(shape=targets[0].shape)
	#define loss function
	loss = k.mean(k.categorical_crossentropy(y_target, model.output))
	#get derivative/gradient of loss wrt input
	loss_gradient = k.gradients(loss, model.inputs)
	#build function
	func = k.function(model.inputs + [y_target], loss_gradient)
	#get gradients
	gradients = []
	start = 0
	end = int(len(targets)/123)
	increment = int(len(targets)/123)
	#loop to avoid OOM error
	for i in range(0, 123):
		tmp = func([np.squeeze(imgs[start:end]), targets[start:end]])[0]
		start = end
		end += increment
		gradients.append(tmp)
	gradients = np.array(gradients)
	print(gradients.shape)
	gradients = np.reshape(gradients, (len(targets), 224, 224, 3))
	return gradients

#helper method to make a target label
def get_target_label(label):
	'''
	label : one-hot vector
			true label of an image
	'''
	true_class = np.argmax(label)
	#push index up by three labels, wrap around in case out of bounds
	target_class = (true_class + 3) % 10
	#convert to one hot vector
	one_hot = np.zeros(10)
	one_hot[target_class] = 1
	return one_hot

def add_imgs(imgs, gradients, epsilon):
	'''
	imgs : numpy array
		   array of original images
	gradients : numpy array
			   array of gradients obtained from fgsm or targeted_fgsm
	epsilon : integer or float
			  number by which to alter the pixel intensities
	'''
	#initialize empty numpy array
	adv_imgs = []
	#create adversarial images here
	for i in range(0, len(imgs)):
		adv = imgs[i] + (np.sign(gradients[i]) * epsilon)
		adv = np.clip(adv, 0, 255)
		adv_imgs.append(adv)
	return adv_imgs

# don't run this again
def fix_channels(name):
	imgs = np.load("data/%s_test_set.npy" % name)
	for i in range(0, len(imgs)):
		imgs[i] = np.clip(imgs[i], 0, 255)
	np.save("data/%s_test_set.npy" % name, imgs)

if __name__ == "__main__":

	model_list = ["subsample", "An Diff", "Med Gauss", "Nonlocal means", "ROF", "TVD"]

	# imgs = np.load("data/%d_test_set.npy" % model_list[i])
	imgs = np.load("data/subsample_test_set.npy")
	print("Loaded in %d images" % len(imgs))
	t_labels = np.load("data/subsample_test_labels.npy")
	# print("Loaded in %d labels" % len(t_labels))
	print("Loaded in %d labels" % len(t_labels))
	labels = np_utils.to_categorical(t_labels, 10)

	base_test = True

	if not base_test:
		for i in range(1, len(model_list)):
			model = load_model("models/%s_vgg16_10_HNN.h5" % model_list[i])
			print("Loaded in %s model" % model_list[i])

			gradients = None
			if not os.path.exists("data/gradients_HNN_%s_filtered_BUG.npy" % model_list[i]):
				print("Did not find %s gradients, creating them now" % model_list[i])
				gradients = FGSM(model, imgs, labels)
				np.save("data/gradients_HNN_%s_filtered_BUG.npy" % model_list[i], gradients)
				print("Made %s gradients" % model_list[i])
			else:
				print("Loaded in %s gradients" % model_list[i])
				gradients = np.load("data/gradients_HNN_%s_filtered.npy" % model_list[i])

			for epsilon in range(1, 11):
				print("Adding e = %d" % epsilon)
				x_adv = add_imgs(imgs, gradients, epsilon)
				if not os.path.exists("FGSM_imgs/og_img_filter_network/%s_BUG" % model_list[i]):
					os.mkdir("FGSM_imgs/og_img_filter_network/%s_BUG" % model_list[i])
				np.save("FGSM_imgs/og_img_filter_network/%s_BUG/adversarial_images_e=%d.npy" % (model_list[i], epsilon), x_adv)
				print("Testing e = %d" % epsilon)
				acc, conf = test_imgs(model, x_adv, labels)
				print("Accuracy: %.4f" % acc)
				print("Confidence: %.4f" % conf)
				del x_adv
			del gradients

			print("Completed %s" % model_list[i])
			sys.exit(0)

	else:
		for i in range(0, len(model_list)):
			acc = []
			method = model_list[i]
			model = load_model("models/%s_vgg16_10_HNN.h5" % method)
			print(method)
			t_acc, conf = test_imgs(model, imgs, labels)
			print(t_acc)
			acc.append(t_acc)
			for epsilon in range(1, 11):
					print("Testing e = %d" % epsilon)
					adv = np.squeeze(np.load("FGSM_imgs/og_img_filter_network/%s/adversarial_images_e=%d.npy" % (method, epsilon)))
					#if (i == 10):
					#	plt.imshow(adv[1])
					t_acc, conf = test_imgs(model, adv, labels)
					print(t_acc)
					acc.append(t_acc)
		np.save("whitebox/clean/before_overall_%s.npy" % method, acc)
		sys.exit(0)


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 17 10:36:44 2018

@author: mohammad
"""
#------------------------------------------------------------------------------
# Import the relevant libraries
#------------------------------------------------------------------------------
import scipy.io
import numpy as np
import random

#------------------------------------------------------------------------------
# Importing the data and the mask
#------------------------------------------------------------------------------
ids = np.load('python_covariates/covars_ids.npy')
covars = np.load('python_covariates/covars.npy')
mask = np.load('python_covariates/mask.npy')

#------------------------------------------------------------------------------
# Fix the names of the files.
#------------------------------------------------------------------------------
newids=[]
for i in range(np.size(ids,0)):
    name = ids[i][0]
    name = name[7:-4]
    newids.append('python_images/' + name + '.npy')
python_file_locs = np.array(newids)
np.save('python_covariates/python_file_locs',python_file_locs)

#------------------------------------------------------------------------------
#
#------------------------------------------------------------------------------
freq_lim = 50
time_cut_off_mins = 72*60
downsample_by = 2
numsamples = 600

for i in range(np.size(ids,0)):

    #load the data
    mat = scipy.io.loadmat(ids[i][0])

    #get the name for saving!
    name = ids[i][0]
    name = name[7:-4]

    # Extract the data
    dat = mat['F']

    #cast any nans as zeros
    dat[np.isnan(dat)] = 0

    # Convert data to float32, and normalize to be between 0 and 1.
    dat = dat.astype('float32')
    dat /=255

    # Apply the mask to the entire dataset
    masknd = np.repeat(mask[:, :, np.newaxis],freq_lim, axis=2)
    masknd = np.repeat(masknd[:, :, :,np.newaxis],np.size(dat,3), axis=3)
    dat = masknd*dat

    delta = np.sum(dat[:,:,0:3,:],2)/4
    theta = np.sum(dat[:,:,4:7,:],2)/4
    alpha = np.sum(dat[:,:,8:15,:],2)/8
    beta  = np.sum(dat[:,:,16:31,:],2)/16

    # Extract the frequency bands
    dat_tmp = np.zeros([np.size(dat,0),np.size(dat,1),4,np.size(dat,3)])
    dat_tmp[:,:,0,:] = delta
    dat_tmp[:,:,1,:] = theta
    dat_tmp[:,:,2,:] = alpha
    dat_tmp[:,:,3,:] = beta
    dat              = dat_tmp

    #downsample
    dat = dat[xrange(0,np.size(dat,0),3),:,:,:]
    dat = dat[:, xrange(0,np.size(dat,1),3),:,:]

    #throw out the unnecesary bits on the tail ends.
    dat = dat[1:9,1:9,:,:]

    # Extract the offset (time between arrest and eeg start time)
    this_offset = covars[i,3]

    # pad the data with zeros so that it is of a consistent length.
    padding_size = int(60*this_offset)
    front_padding = np.zeros([8,8,4,padding_size])

    padding_size = time_cut_off_mins - (int(60*this_offset) + np.size(dat,3))
    if padding_size > 0:
        end_padding = np.zeros([8,8,4,padding_size])
        dat = np.append(front_padding,dat,axis=3)
        dat = np.append(dat,end_padding,axis=3)
    else:
        dat = np.append(front_padding,dat,axis=3)
        dat = dat[:,:,:,0:time_cut_off_mins]

    # transform data to keras firendly format
    dat = dat.transpose(3,0,1,2)
    dupdat = np.zeros([numsamples,72,8,8,4])

    #how many hours of data do we have?
    for n in range(0,numsamples):
        base = 0
        t_index = []
        for t in range(0,72):
            t_index.append(random.randint(0,59)+base)
            base = base + 60

        dupdat[n,:,:,:,:] = dat[np.array(t_index),:,:,:]

    dat = dupdat

#     # Visualize the images.
#    fig,axarr = plt.subplots(2, 2, dpi=100)
#    axarr[0,0].imshow(dat[20,9,:,:,0])
#    axarr[0,0].set_title('delta power', fontsize=8)
#    axarr[0,1].imshow(dat[20,9,:,:,1])
#    axarr[0,1].set_title('theta power', fontsize=8)
#    axarr[1,0].imshow(dat[20,9,:,:,2])
#    axarr[1,0].set_title('alpha power', fontsize=8)
#    axarr[1,1].imshow(dat[20,9,:,:,3])
#    axarr[1,1].set_title('beta power', fontsize=8)

###############################################################################
## TRAINING DATA ##############################################################
# Subjects x Time x rows x columns x Hertz
###############################################################################
    np.save('python_images/' + name,dat)


## 1.1 Types
from deap import base, creator
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)

## 1.2 Initialization
import random
from deap import tools

IND_SIZE = 10

toolbox = base.Toolbox()
toolbox.register("attribute", random.random)
toolbox.register("individual", tools.initRepeat, creator.Individual,
                 toolbox.attribute, n=IND_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

## 1.3 Operators
def evaluate(individual):
    return sum(individual),

toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.1)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", evaluate)

## 1.4 Algorithms
def main():
    pop = toolbox.population(n=50)
    CXPB, MUTPB, NGEN = 0.5, 0.2, 40

    # Evaluate the entire population
    fitnesses = map(toolbox.evaluate, pop)
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    for g in range(NGEN):
        # Select the next generation individuals
        offspring = toolbox.select(pop, len(pop))
        # Clone the selected individuals
        offspring = map(toolbox.clone, offspring)

        # Apply crossover and mutation on the offspring
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # The population is entirely replaced by the offspring
        pop[:] = offspring

    return pop

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
    Time: 2018-10-01
    Author: Tsystem
"""
import json
from django.http import HttpResponse

def handle_category(request):
    resp = {
          "status": 200,
          "message": "success",
          "data": [
            {
              "name": "DDOS",
              "percent": 20
            },
            {
              "name": "木马病毒",
              "percent": 50
            },
            {
              "name": "远程执行",
              "percent": 80
            },
            {
              "name": "注入攻击",
              "percent": 40
            }
          ]
        }

    return HttpResponse(json.dumps(resp), content_type="application/json")


# 把文件2的内容接在文件1的后面
import os
import re
import sys

def concat(file1, file2):
    fileList = os.listdir(file2)  # 待修改文件夹
    # os.chdir(file2)  # 将当前工作目录修改为待修改文件夹的位置
    for fileName in fileList:  # 遍历文件夹中所有文件
        f1 = file1 + fileName
        f2 = file2 + fileName
        with open(f2, "r") as file:
            a = file.read()
        file.close()
        with open(f1, "a") as file:
            file.write(a)
        file.close()
        print(fileName)
    sys.stdin.flush()  # 刷新

# file1 = r"F:/ZYY/chess/images/images1368-3072/lab_loc/1-1-corner/"
# file2 = r"F:/ZYY/chess/images/images1368-3072/lab_loc/1-2-corner/"
# file = "F:/ZYY/chess/images/images1368-3072/lab_loc/corner/"

file = "F:/ZYY/chess/images/train/Annotation/lab_loc/corner/"
file1 = r"F:/ZYY/chess/images/train/Annotation/lab_loc/1-1-corner/"
file2 = r"F:/ZYY/chess/images/train/Annotation/lab_loc/1-2-corner/"
# file3 = r"F:/ZYY/chess/images/train/Annotation/lab_loc/2-1-corner/"

concat(file1, file) # 后面接在前
concat(file2, file)
# concat(file3, file) # 后面接在前


import re
import emoji
from bs4 import BeautifulSoup
from soynlp.normalizer import repeat_normalize

from typing import List

from LAC import LAC

lac = LAC(mode='lac', use_cuda=False)

class Transform(object):
    def __init__(self,
                 tokenizer,
                 args):

        self.args = args
        self.tokenizer = tokenizer

        # stopwords
#         with open(f'{args.tokenizer_path}/STOPWORDS.txt') as f:
#             self.STOPWORDS = [s.strip() for s in f.readlines()]

        # regular expression
        self.html = re.compile("<(\"[^\"]*\"|'[^']*'|[^'\">])*>")
        emojis = ''.join(emoji.UNICODE_EMOJI.keys())
        self.zh_pattern = re.compile(f'[^ .,?!/@$%~％·∼()。、，《 》“”：0-9a-zA-Z\u4e00-\u9fff{emojis}]+') # 기호, 영어, 중국어, 이모티콘
        self.url = re.compile(r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)')
        self.email = re.compile('([0-9a-zA-Z_]|[^\s^\w])+(@)[a-zA-Z]+.[a-zA-Z)]+')

        self.emo = emoji.get_emoji_regexp()
        self.image = re.compile(r'(\[image#0\d\])')
        self.hashtag = re.compile(r'#(\w+)')

    def _strpreprocess(self, string):
        """모델링용 텍스트 전처리 (중국어 키워드 추출에 적합하게 변경 필요함.)"""

        # compile basics

        url = re.compile(r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)')
        email = re.compile('([0-9a-zA-Z_]|[^\s^\w])+(@)[a-zA-Z]+.[a-zA-Z)]+')
        emojis = ''.join(emoji.UNICODE_EMOJI.keys())
        zh_pattern = re.compile(f'[^ .,?!/@$%~％·∼()。、，《 》“”：0-9a-zA-Z\u4e00-\u9fff{emojis}]+') # 기호, 영어, 중국어, 이모티콘

        # process
        if self.html.search(string) != None: # html js 처리
            soup = BeautifulSoup(string, "lxml")
            for script in soup(["script", "style"]):
                script.decompose()
            string = soup.get_text()
        string = string.strip()
        string = self.zh_pattern.sub(' ', string) # 숫자, 문자, whitespace, 이모지, 일반특수문자를 제외한 모든 유니코드 제거.
        string = re.sub(' ',' ', string) #  제거
        string = repeat_normalize(string, num_repeats=3) # repeats
        string = self.url.sub(' [URL] ', string) # url
        string = self.email.sub(' [EMAIL] ', string) # email
        # 요기에 보존리스트 추가하자.
        string = re.sub(r'\s+',' ', string) #white space character 변환 연속 하나로
        return string.strip()

    def _get_keywords(self, token_list: List[str]) -> List[str]:
        """
        step 1) stopwords 제외하기.
        step 2) pos == L part일 경우, Punctuation을 제외함.
        pos 관련 필터는 이 내부에 구현?
        """
        keywords = []
        for token, pos in zip(token_list, pos):
            if token in self.STOPWORDS:
                continue
            elif pos == 'L':
                """
                tokenizer에서 후처리로 하나의 토큰안에 문자 + 구둣점의 케이스를 분리함. 그러므로 full match.
                """
                # if re.fullmatch(rf'[{string.punctuation}]+',token) == None: # punctuation case의 경우.
                if token.strip(string.punctuation) == '':
                    continue
                else:
                    keywords.append(token)
            else:
                continue

        return keywords

    def _gen_ngrams(self, doc, n_gram=1):
        ngrams = zip(*[doc[i:] for i in range(n_gram)])
        return [" ".join(ngram) for ngram in ngrams]

    def keyword_test(self, doc):
        return

    def transform(self, doc):
        pass


def say_hello():
    print('Hello!')

def sum(a, b):
    return a + b

say_hello()

s = sum(5, 8)
print(s)

# Note one of the interesting capabilities in Python - because the + operator
# works as concatenation and addition, we can use the sum function on both str
# types and int types (as well as float types).

sentence = sum('this is ', 'a sentence')
print(sentence)

# Note that you can also name parameters when you call a function; this allows
# additional flexibility in parameter ordering.

s = sum(b='This', a='That')
print(s)


# in una gara sono state assegnate le pettorine da 1 a 50
# i primi 3 classificati sono il 15, 25, 38
# ciclare (tramite for) su tutti i partecipanti e stampare i 3 vincitori

vincitori = [15, 25, 38]
vincitori_individuati = 0
for pettorina in range(1,51):
    if pettorina in vincitori:
        print(f'il {pettorina} ha vinto!!')
        vincitori_individuati += 1
    if vincitori_individuati > 2:
        break

vincitori = [15, 25, 38]
podio = list()
for pettorina in range(1,51):
    if pettorina in vincitori:
        print(f'il {pettorina} ha vinto!!')
        podio.append(pettorina)
    if len(podio) > 2:
        break

# eseguire il medesimo esercizio tramite while
vincitori = [15, 25, 38]
pettorina = 1
while pettorina <= len(range(1,51)):
    if pettorina in vincitori:
        print(f'il {pettorina} ha vinto!!')
    pettorina += 1

# verificare di non sprecare inutilmente tempo e cicli aggiungendo break


a = int(input())
b = 0
while a > b:
    c = int(input())
    if c % 2 == 0:
        print('even')
    else:
        print('odd')

a = 1000
while a > 0:
    for x in range(0, 5):
        print(a, end='\t' )
        a = a - 1
    print('')

a = int(input())
b = 1
while b <= a:
    c = int(input())
    e = 1
    print("Case %d:" %b, end='')
    while e <= c:
        if c % e == 0:
            print(" %d" %e, end='')
        e = e + 1
    print('')
    a = a - 1

a = int(input())
b = 1
while b <= a:
    c = int(input()) # 56789
    last = c % 10
    while c > 0:
        first = c % 10
        c = c // 10
    sum = first + last
    print('Sum = %d' %sum)
    b = b + 1


from math import *
values = [
-9.631718184508847,
-9.807692307692307
]
errors = [
0.07374920079720412,
0.33670225101017276
]

def mean(wart, niep):
    licz, mian = 0,0
    for w,n in zip(wart, niep):
        licz += w/n**2
        mian += 1/n**2
    return licz/mian

def u_int(wart,niep):
    mian = 0
    for n in niep:
        mian += 1/(n)**2
    return 1/sqrt(mian)

def u_ext(wart,niep):
    summa = 0
    mean_val = mean(wart, niep)
    for w,n in zip(wart, niep):
        summa += ((w-mean_val)/n)**2
    return sqrt(((u_int(wart,niep)**2)/(len(wart)-1))  *  summa)

print('Mean: ',mean(values,errors))
print('U_int:',u_int(values,errors))
print('U_ext:',u_ext(values,errors))


import socket

class Client:
    def __init__(self):
        self.IP = socket.gethostbyname(socket.gethostname())
        self.PORT = 8000
        self.ADDR = (self.IP, self.PORT)
        self.FORMAT = 'utf-8'
        self.LENGTH = 64
        self.client_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)

    def connect_with_server(self):
        self.client_socket.connect(self.ADDR)

    def send_message(self, message):
        message = message.encode(self.FORMAT)
        message_length = len(message)
        message_length = str(message_length).encode(self.FORMAT)
        message_length += b' ' * (self.LENGTH - len(message_length))
        self.client_socket.send(message_length)
        self.client_socket.send(message)

    def close_connection(self):
        self.client_socket.close()

client = Client()
client.connect_with_server()
client.send_message("is maria")
client.send_message("alive and well?")
client.send_message("DISCONNECT")
client.close_connection()


#! /usr/bin/python3
r"""模块multi_memu是用于在有多个层级的菜单中来回切换。

Raises:
    IndexError: Sequence index out of range.
"""

menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车站': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}

current_layer = menu  # 设置当前层
layer_list = list()  # 存放上一层
while 1:
    for layer in current_layer:
        print(layer)
    city = input('Please enter a city(q to quit, b to back): ').strip()
    if city.upper() == 'Q':
        print('Thank you for your use. Please call again.')
        break
    elif city.upper() == 'B':
        if not layer_list:
            print('You\'re already on top!')
        else:
            current_layer = layer_list.pop()  # 取出上一层，并删除
    else:
        if city in current_layer:
            layer_list.append(current_layer)  # 将当前层加入到列表
            current_layer = current_layer[city]  # 重新定义当前层，当前层的下一层
        else:
            print('Sorry, your inputs are out of the operating range.')


from random import random

class Monster:
    def __init__( self, MonsterName ):
        self.MonsterName = MonsterName

        if MonsterName == "Golem":
            self.intro = """\nAs you step through the portal you glance around to see a perfectly circular chamber lit evenly by the orange glow of torchlight.
As you step forward you feel the ground tremble beneath your feet and dust fall from the ceiling.
The sound of splitting rock greets your ears as a humanoid creature composed entirely of rock pulls itself from the ground.
The Golem stands and a roar of challenge emanates from its immense chest as it rushes you."""
            self.MonsterHealth = 40
            self.MonsterStartingHealth = 40
            self.attackDamage = 10
            self.criticalPct = float(.01)
            self.dodgePct = float(.005)
            self.attackMethod = "Swings its fist"
        elif MonsterName == "Skeleton":
            self.intro = """\nAs you step through the portal you observe an eerily dark chamber lit by a single purple lantern.
As you step forward you hear the grate of bone on bone as Skeleton clad in a rusted breastplate steps forth.
Your ears ache from the sound of the iron swordtip that drags the ground in its hands and is slowly raised to attack.
The Skeleton bones become a cacaphony of rattles as it bears down on you eager to begin."""
            self.MonsterHealth = 25
            self.MonsterStartingHealth = 25
            self.attackDamage = 8
            self.criticalPct = float(.02)
            self.dodgePct = float(.03)
            self.attackMethod = "Slashes its sword"
        elif MonsterName == "Wisp":
            self.intro = """\nAs you step through the portal you find a simple chamber illuminated by a drifting and slowly pulsing yellow and green light.
The light begins pulsing rapidly and it ceases its movement at the sound of the portal.
Upon closer observation you see that the light is that of a Wisp and its light takes on a blood-red hue.
A small discharge of energy is your only warning as the Wisp zips toward you ready for its meal."""
            self.MonsterHealth = 20
            self.MonsterStartingHealth = 20
            self.attackDamage = 5
            self.criticalPct = float(.03)
            self.dodgePct = float(.10)
            self.attackMethod = "Sucks away your lifeforce"
        else:
            self.intro = """\nAs you step through the portal you are greeted to the sight of a decrepit stone tomb whose slab lays forgotten on the floor.
Suddenly, a high pitched scream emanates from the tomb and you see a decaying hand grasp the tomb from within and a tattered figure slowly rise.
A sudden cold surrounds you as the Lich climbs forth and begins the mirthless laugh of those damned for all eternity.
Dark energy pools around its hands and seems to flow and drip like water as the Lich advances."""
            self.MonsterHealth = 50
            self.MonsterStartingHealth = 50
            self.attackDamage = 13
            self.criticalPct = float(.03)
            self.dodgePct = float(.10)
            self.attackMethod = "Blasts forth necrotic energy"

    def getAttackMethod(self):
        return self.attackMethod

    def getStartingHealth(self):
        return self.MonsterStartingHealth

    def getIntro(self):
        print(self.intro)

    def getName( self ):
        return self.MonsterName

    def getHealth(self):
        return self.MonsterHealth

    def getDamage( self ):
        ChanceToCrit = random()
        if ChanceToCrit <= self.criticalPct:
            return self.attackDamage * 3
        else:
            return self.attackDamage

    def getDodge(self):
        Dodge = False
        ChancetoDodge = random()
        if ChancetoDodge <= self.dodgePct:
            Dodge = True
        return Dodge

    def lowerHealth(self, damageAmt):
        self.MonsterHealth = self.MonsterHealth - damageAmt

    def setHealth(self, HealthLevel):
        self.MonsterHealth = HealthLevel

    def restoreMonsterFullHealth(self):
        self.MonsterHealth = self.MonsterStartingHealth

    def __repr__(self):
        return "{0}, {1} Damage,, {2} Health".format(self.MonsterName, self.attackDamage, self.MonsterHealth)


import pickle
from utils import yaml_load

def get_numpy_word_embed():
    row = 0

    config = yaml_load("config.yaml")
    model_cfg = config.get("model", {})
    data_cfg = config.get("data", {})
    glove_path = model_cfg["glove_path"]
    glove_length = model_cfg["glove_length"]
    vocab_file = data_cfg["vocab_file"]

    words_embed = {}
    with open(glove_path, mode='r')as f:
        lines = f.readlines()
        for line in lines:
            line_list = line.split()
            word = line_list[0]
            embed = line_list[1:]
            embed = [float(num) for num in embed]
            words_embed[word] = embed
            if row > 20000:
                break
            row += 1

    word2idx = {}
    with open(vocab_file, 'rb') as handle:
        word2idx = pickle.load(handle)
    idx2word = {idx: w for w, idx in word2idx.items()}
    id2emb = {}
    id2emb[0] = [0.0] * glove_length
    for (_, idx) in word2idx.items():
        if idx2word[idx] in words_embed:
            id2emb[idx] = words_embed[idx2word[idx]]
        else:
            id2emb[idx] = [0.0] * glove_length
    data = [id2emb[idx] for idx in range(len(word2idx) + 1)]

    return data


#!/usr/bin/env python
"""
Reduce samples that have too high energies by comparing
between the same group of samples.
The group is defined according to the name of directory before the last 5 digits.
For example, the directory `smpl_XX_YYYYYY_#####` where `#####` is the
last 5 digits and the group name would be `smpl_XX_YYYYY`.

Usage:
  reduce_high_energy_samples.py [options] DIRS...

Options:
  -h,--help  Show this message and exit.
  -o OUT     Output file name. [default: out.high_energy_samples]
  --threshold=THRESHOLD
             Threshold of energy/atom that determines high energy samples.
             [default: 1.0]
"""
from __future__ import print_function

import os,sys
from docopt import docopt
from datetime import datetime

from nappy.napsys import NAPSystem

__author__ = "RYO KOBAYASHI"
__version__ = "160727"

def get_obsolete_dirname():
    prefix = "obsolete_"
    today = datetime.today()
    return prefix+today.strftime("%y%m%d")

def get_groups(smpldirs):
    groups = {}
    ns = len(smpldirs)
    if ns < 100:
        ms = 1
    else:
        ms = ns/100
    for i,s in enumerate(smpldirs):
        if i%ms == 0:
            print('.',end=".")
        try:
            with open(s+'/erg.ref','r') as f:
                erg = float(f.readline())
        except:
            print('Failed to read erg.ref, so skip '+s)
            continue
        key = s[:-6]
        if not key in groups:
            groups[key] = []
        groups[key].append([s,erg])
    print('')
    return groups

def get_list_high_energy(gsmpls,threshold):
    emin = 1e+30
    highsmpls = []
    ergs = []
    for i,s in enumerate(gsmpls):
        smpldir = s[0]
        erg = s[1]
        #atoms = read(smpldir+'/POSCAR',format='vasp')
        atoms = NAPSystem(fname=smpldir+"/pos",format='pmd')
        natm = atoms.num_atoms()
        erg /= natm
        ergs.append(erg)
        emin = min(erg,emin)
    for i,s in enumerate(gsmpls):
        smpldir = s[0]
        erg = ergs[i]
        if erg-emin > threshold:
            highsmpls.append(smpldir)
    return highsmpls

if __name__ == "__main__":

    args = docopt(__doc__)
    smpldirs = args['DIRS']
    outfname = args['-o']
    threshold = float(args['--threshold'])

    print('grouping samples...')
    groups = get_groups(smpldirs)

    print('looking for high-energy samples...')
    highsmpls = []
    for g,smpls in groups.items():
        print('.',end='')
        highsmpls.extend(get_list_high_energy(smpls,threshold))
    print('')

    with open(outfname,'w') as f:
        for s in highsmpls:
            f.write(s+'\n')
    print('number of samples to be reduced = ',len(highsmpls))
    print('check '+outfname+' and run the following commands:')
    print('')
    # obsdir = get_obsolete_dirname()
    # print('  mkdir '+obsdir)
    # print('  for d in `cat '+outfname+'`; do mv $d '+obsdir
    #       +'/; done')
    # print('')


import setuptools

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

setuptools.setup(
    name="initpyproj",
    version="1.0.0",
    author="Nils Urbach",
    author_email="ndu01u@gmail.com",
    description="Initialize an empty python project; make it a git repo; and sync it with a new GitHub repo",
    long_description=long_description,
    long_description_content_type="text/markdown",
    license="MIT",
    keywords=[],
    url="https://github.com/Schnilsibus/initpyproj",
    package_dir={"": "_core"},
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Natural Language :: English",
        "Programming Language :: Python"
    ],
    test_suite="tests",
    include_package_data=True,
    package_data={"templates": ["[template]*"]}
)


import uuid

from click.testing import CliRunner
import pytest

from kubetemp.cli import cli

@pytest.fixture
def runner():
    return CliRunner()

@pytest.mark.parametrize('name', ['Joe', 'John', 'Jim', 123, ''])
def test_param_arguments(runner, name):
    result = runner.invoke(
        cli,
        ['tests/files/test.tmpl', '-p', 'name', name]
    )
    assert result.exit_code == 0
    assert result.output == f'Hello, {name}!\n'

@pytest.mark.parametrize('ext', ['json', 'yaml', 'yml'])
def test_params_from_file(runner, ext):
    result = runner.invoke(
        cli,
        ['tests/files/test.tmpl', '-f', f'tests/files/params.{ext}']
    )
    assert result.exit_code == 0
    assert result.output == f'Hello, Tester!\n'

@pytest.mark.parametrize('ext', ['json', 'yaml', 'yml'])
def test_override_cli_params(runner, ext):
    result = runner.invoke(
        cli,
        [
            'tests/files/test.tmpl',
            '-f', f'tests/files/params.{ext}',
            '-p', 'name', 'Joe',
        ]
    )
    assert result.exit_code == 0
    assert result.output == f'Hello, Joe!\n'

@pytest.mark.parametrize('name', ['Joe', 'John', 'Jim', 123, ''])
def test_stdin_template(runner, name):
    result = runner.invoke(
        cli,
        ['-', '-p', 'name', name],
        input=f'How are you, {name}?'
    )
    assert result.exit_code == 0
    assert result.output == f'How are you, {name}?\n'

def test_write_output(runner, tmpdir):
    path = tmpdir.join('test.txt')
    hash_str = str(uuid.uuid4())
    result = runner.invoke(
        cli,
        [
            'tests/files/test.tmpl',
            '-p', 'name', hash_str,
            '-o', str(path)
        ]
    )
    assert result.exit_code == 0
    assert result.output == ''
    assert path.read() == f'Hello, {hash_str}!'


from typing import Any, Dict, Iterable, List, Tuple

from ..common import (
    IDENTITY_FACTORY,
    IS_NOT_CALLABLE,
    IS_STR,
    VALUE,
    Predicate,
    Transform,
)
from ..services import Scoped
from .middleware import APPLY_INTO, Context, Middleware, WithMiddlewares

class Name(Middleware, WithMiddlewares):
    @classmethod
    def verify(cls, value: Any) -> None:
        super().verify(value)

        if IS_NOT_CALLABLE(value):
            raise TypeError(f"{value} should be either a callable or a string")

    @classmethod
    def _get_simplifications(cls) -> List[Tuple[Predicate, Transform]]:
        simplifications = super()._get_simplifications()

        simplifications.append((IS_STR, IDENTITY_FACTORY))
        return simplifications

    def _init_middleware_types(self) -> None:
        super()._init_middleware_types()

        from .action import Action

        self.middleware_types.append(Action)

    def _init_middleware_apply_methods(self) -> None:
        super()._init_middleware_apply_methods()
        self.middleware_apply_methods.append(APPLY_INTO)

    def create_middlewares_from(self, value: Dict[str, Any]) -> Iterable[Middleware]:
        for middleware_type, middleware_name in zip(
            self.middleware_types, self.get_middleware_names()
        ):
            # assert middleware_type is Action
            new_value = {middleware_name: value}
            yield middleware_type(new_value)

    def _do_parse(self, value: Dict[str, Any]) -> Dict[str, List[Middleware]]:
        self.verify(value)

        return {"middlewares": list(self.create_middlewares_from(value))}

    async def get_register_name(self, context: Context) -> str:
        coroutine = WithMiddlewares.apply(self, context.replace_with_void_next())
        await coroutine()

        return context.scoped.getmagic(VALUE)

    async def _do_apply(self, context: Context) -> Scoped:
        name = await self.get_register_name(context)

        context.scoped.setmagic(NAME, name, 1)

        await context.next()
        return context.scoped

NAME = Name.get_name()


# services/server/project/api/libraries.py

from flask import Blueprint, request
from flask_restful import Resource, Api

from project import db
from project.api.models import Library

from project.api.functions import get_pypi_version

from sqlalchemy import exc

libraries_blueprint = Blueprint('libraries', __name__)
api = Api(libraries_blueprint)

class ServerHome(Resource):
    def get(self):
        return {
            'message': 'welcome!',
        }

class LibrariesList(Resource):
    def get(self):
        page = request.args.get('page', default=1, type=int)
        libraries = Library.query.order_by(Library.id.desc()).paginate(page, 25, False)
        response_object = {
            'status': 'success',
            'libraries': [library.to_json() for library in libraries.items]
        }
        return response_object, 200

class Libraries(Resource):
    def get(self, library_id):
        """Get single library details"""
        response_object = {
            'status': 'fail',
            'message': 'Library does not exist'
        }
        try:
            library = Library.query.filter_by(id=int(library_id)).first()
            if not library:
                return response_object, 400
            else:
                response_object = {
                    'status': 'success',
                    'library': library.to_json()
                }

                response_object = get_pypi_version(response_object)

                return response_object, 200
        except ValueError:
            return response_object, 400

    def put(self, library_id):
        """Update exisiting Library"""
        put_data = request.get_json()

        response_object = {
            'status': 'fail',
            'message': 'Invalid payload.'
        }

        if not put_data:
            return response_object, 400

        name = put_data.get('name')
        repository = put_data.get('repository')
        download_count = put_data.get('download_count')
        issue_count = put_data.get('issue_count')
        author = put_data.get('author')
        user_rating = put_data.get('user_rating')

        updated_library = Library.query.filter_by(id=library_id).first()

        if updated_library:
            if name:
                updated_library.name = name
            if repository:
                updated_library.repository = repository
            if download_count:
                updated_library.download_count = download_count
            if issue_count:
                updated_library.issue_count = issue_count
            if author:
                updated_library.author = author
            if user_rating:
                updated_library.user_rating = user_rating

            db.session.commit()
            response_object['status'] = 'success'
            response_object['message'] = f'Library {name} was updated!'
            return response_object, 204

        response_object['message'] = 'Sorry. That library does not exist'
        return response_object, 400

    def delete(self, library_id):
        """Delete exisiting Library"""
        Library.query.filter_by(id=library_id).delete()
        db.session.commit()
        response_object = {
            'status': 'success',
            'message': 'Library deleted'
        }
        return response_object, 200

class NewLibrary(Resource):
    def post(self):
        """Create new library"""
        post_data = request.get_json()

        response_object = {
            'status': 'fail',
            'message': 'Invalid payload.'
        }

        if not post_data:
            return response_object, 400

        name = post_data.get('name')
        repository = post_data.get('repository')
        download_count = post_data.get('download_count')
        issue_count = post_data.get('issue_count')
        author = post_data.get('author')
        user_rating = post_data.get('user_rating')

        try:
            library = Library.query.filter_by(name=name, repository=repository).first()
            if not library:
                db.session.add(
                    Library(
                        name=name,
                        repository=repository,
                        download_count=download_count,
                        issue_count=issue_count,
                        author=author,
                        user_rating=user_rating
                    )
                )
                db.session.commit()
                response_object['status'] = 'success'
                response_object['message'] = f'Library {name} was added!'
                return response_object, 201
            else:
                response_object['message'] = 'Sorry. That library already exists.'
                return response_object, 400
        except exc.DataError:
            db.session.rollback()
            return response_object, 400

api.add_resource(ServerHome, '/')
api.add_resource(LibrariesList, '/libraries')
api.add_resource(Libraries, '/library/<int:library_id>')
api.add_resource(NewLibrary, '/library')


"""

非常简单的10行代码推理MaskRCNN
"""

import torchvision
from PIL import Image
from torchvision.transforms import transforms as T
import cv2
from alfred.vis.image.common import get_unique_color_by_id
import numpy as np
import random
import glob
import os

COCO_INSTANCE_CATEGORY_NAMES = [
    '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
    'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
    'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
    'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A',
    'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
    'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
    'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
    'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
    'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',
    'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
    'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
    'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]

model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
model.eval()
print('MaskRCNN model loaded.')

def random_colour_masks(image):
    colours = [[0, 255, 0], [0, 0, 255], [255, 0, 0], [0, 255, 255], [255, 255, 0], [
        255, 0, 255], [80, 70, 180], [250, 80, 190], [245, 145, 50], [70, 150, 250], [50, 190, 190]]
    r = np.zeros_like(image).astype(np.uint8)
    g = np.zeros_like(image).astype(np.uint8)
    b = np.zeros_like(image).astype(np.uint8)
    r[image == 1], g[image == 1], b[image ==
                                    1] = colours[random.randrange(0, 10)]
    coloured_mask = np.stack([r, g, b], axis=2)
    return coloured_mask

def instance_segmentation_api(img_path, threshold=0.5, rect_th=3, text_size=3, text_th=3):
    masks, boxes, pred_cls = get_prediction(img_path, threshold)
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    for i in range(len(masks)):
        rgb_mask = random_colour_masks(masks[i])
        img = cv2.addWeighted(img, 1, rgb_mask, 0.5, 0)
        cv2.rectangle(img, boxes[i][0], boxes[i][1],
                      color=(0, 255, 0), thickness=rect_th)
        cv2.putText(img, pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX,
                    text_size, (0, 255, 0), thickness=text_th)
    cv2.imshow('aa', img)
    cv2.waitKey(0)

def get_prediction(img_path, threshold):
    img = Image.open(img_path)
    transform = T.Compose([T.ToTensor()])
    img = transform(img)
    pred = model([img])
    pred_score = list(pred[0]['scores'].detach().numpy())
    pred_t = [pred_score.index(x) for x in pred_score if x > threshold][-1]
    masks = (pred[0]['masks'] > 0.5).squeeze().detach().cpu().numpy()
    pred_class = [COCO_INSTANCE_CATEGORY_NAMES[i]
                  for i in list(pred[0]['labels'].numpy())]
    pred_boxes = [[(i[0], i[1]), (i[2], i[3])]
                  for i in list(pred[0]['boxes'].detach().numpy())]
    masks = masks[:pred_t+1]
    pred_boxes = pred_boxes[:pred_t+1]
    pred_class = pred_class[:pred_t+1]
    return masks, pred_boxes, pred_class

if __name__ == '__main__':
    imgs = glob.glob(os.path.join('images/*.png'))
    for img in imgs:
        instance_segmentation_api(img)


import pandas as pd
import os
import re
from datetime import datetime

EV_PATTERN = re.compile('^ev_dump_.*\.csv$', re.IGNORECASE)
DEVICE_SCHEMA = {
    'timestamp': 'str',
    'device_id': 'str',
    'event_type': 'str',
    'event_payload': 'str'
}

def __clean_timestamp(rawValue:str) -> datetime:
    try:
        if rawValue is not None:
            actualVal = float(rawValue)
            return datetime.fromtimestamp(actualVal)
    except Exception:
        pass

    return None

def __clean_device_id(rawValue:str) -> str:
    try:
        if rawValue is not None and len(rawValue) == 8:
            # parse as hex into decimal to confirm its a valid value
            _ = int(rawValue, 16)
            return rawValue.lower()
    except Exception:
        pass

    return None

def __clean_event_type(rawValue:str) -> str:
    if rawValue is None or len(rawValue) >= 256:
        # unexpected value, lets skip it
        return None

    return rawValue.lower()

def get_ev_data(rootDir:str) -> pd.DataFrame:
    """Retrieves all EV data from the given folder and returns as a DataFrame."""

    combinedDf = None

    for fileName in os.listdir(rootDir):
        absFilePath = os.path.join(rootDir, fileName)
        if not os.path.isfile(absFilePath):
            continue # assuming all files are within the given directory and not subdirectories.

        if not re.match(EV_PATTERN, fileName):
            # not an ev file - skip for now
            continue

        df = pd.read_csv(absFilePath, sep=',', skiprows=1, skip_blank_lines=True, header=None, names=DEVICE_SCHEMA.keys(), dtype=DEVICE_SCHEMA, on_bad_lines='warn')
        df['timestamp'] = df['timestamp'].apply(__clean_timestamp)
        df['device_id'] = df['device_id'].apply(__clean_device_id)
        df['event_type'] = df['event_type'].apply(__clean_event_type)
        df.dropna(inplace=True)

        if combinedDf is None:
            combinedDf = df
        else:
            combinedDf = pd.concat([combinedDf, df])

    return combinedDf


"""
Ejercicio 5 implementado con diccionarios
"""

personas = ['Juan', 'Ana', 'Pedro', 'Camila', 'Julio', 'María']
comunas = ['Las Condes', 'Ñuñoa', 'Providencia', 'Las Condes', 'Ñuñoa', 'La Florida']

def crear_listas_por_comunas(personas, comunas) :
    dic = {}
    for i, comuna in enumerate(comunas) :
        if comuna in dic :
            dic[comuna].append(personas[i])
        else :
            dic[comuna] = [personas[i]]
    return dic

print('--------------input--------------------')
print('personas: {}'.format(personas))
print('comunas: {}'.format(comunas))
mi_dic = crear_listas_por_comunas(personas, comunas)
print('--------------------output--------------')
for key in mi_dic:
    print('Comuna: {}'.format(key))
    print(mi_dic[key])


import string

# Representa os caracteres aceitos durante no lexer
caracteres = string.ascii_letters + 'ãâáàéèêíìîçõóòôúùûÃÂÁÀÉÈÊÍÌÎÇÕÓÒÔÚÙÛ_:!.'
# Representa os dígitos aceitos durante no lexer
digitos = string.digits
# Lista de operadores relacionais aceitos no lexer
op_relacionais = ['>', '<', '>=', '<=', '==', '!=']
# Representa os dígitos e pontuação aceitos durante no lexer para formar um valor numérico.
valor_numerico = digitos + '.'
# Representa tudo aceita na formação de um texto (string)
valor_texto = caracteres + ' '

# Representa que o valor do token é inteiro
inteiro = 'v_inteiro'
# Representa que o valor do token é flutuante
flutuante = 'v_flutuante'
# Representa que o valor do token é um texto
texto = 'v_texto'
# Representa o valor Verdadeiro (bit 1)
verdadeiro = 'Verdadeiro'
# Representa o valor Falso (bit 0)
falso = 'Falso'

todos = [inteiro, flutuante, texto]


import praw
import configparser
import time
import datetime
from praw.models import MoreComments

cp = configparser.RawConfigParser()
configFilePath = './client.config'
cp.read(configFilePath)

current_client_id = cp.get('client-config', 'client_id')
current_client_secret = cp.get('client-config', 'client_secret')

reddit = praw.Reddit(
    user_agent="Comment Extraction (by u/chriskok1337)",
    client_id=current_client_id,
    client_secret=current_client_secret
)

# Time to rest between reddit random post requests
TIME_BETWEEN_REQUESTS = 2.1

"""Gets and returns formatted subreddit metadata

    Args:
        subreddit (str): the subreddit we want to get metadata for

    Returns:
        subreddit_dict: a dictionary of relevant metadata for the subreddit
"""
def get_subreddit_meta(subreddit):
    subreddit_dict = {}
    subreddit = reddit.subreddit(subreddit)

    subreddit_dict['id'] = subreddit.id
    subreddit_dict['created_utc'] = subreddit.created_utc
    subreddit_dict['year'] = datetime.date.fromtimestamp(subreddit.created_utc).year
    subreddit_dict['description'] = subreddit.description
    subreddit_dict['display_name'] = subreddit.display_name
    subreddit_dict['name'] = subreddit.name
    subreddit_dict['subscribers'] = subreddit.subscribers
    subreddit_dict['public_description'] = subreddit.public_description

    return subreddit_dict

"""Extracts and processes relevant information for a submission

    Args:
        sub (Submission): the submission we're extracting data from
        comment_sort (str): sorting order for comments [confidence, controversial, new, old, q&a, top]

    Returns:
        sub_dict: a dictionary of relevant information for this submission
"""
def save_submission(sub, comment_sort='top'):
    sub_dict = {}
    sub_dict['id'] = sub.id
    sub_dict['num_comments'] = sub.num_comments
    sub_dict['score'] = sub.score
    sub_dict['selftext'] = sub.selftext
    sub_dict['stickied'] = sub.stickied
    sub_dict['title'] = sub.title
    sub_dict['upvote_ratio'] = sub.upvote_ratio
    sub_dict['created_utc'] = sub.created_utc
    sub_dict['year'] = datetime.date.fromtimestamp(sub.created_utc).year

    current_top_comments = []
    # Set comment sort to best before retrieving comments
    sub.comment_sort = comment_sort
    # Limit to, at most, 10 top level comments
    sub.comment_limit = 10
    for top_level_comment in sub.comments:
        if isinstance(top_level_comment, MoreComments):
            continue
        current_top_comments.append(top_level_comment.body)

    sub_dict['top_comments'] = current_top_comments

    return sub_dict

"""Gets and returns formatted TOP posts (and related comments) by subreddit

    Args:
        subreddit (str): the subreddit we want to scrape posts for
        limit (int): limit to the number of submissions
        time_range (str): how far back to look for gathering data [all, day, hour, month, week, year]

    Returns:
        post_dict: a dictionary of subreddit submission dictionaries. ID as the key and relevant data stored as the value.
"""
def get_top_posts(subreddit, limit=None, time_range="all"):
    submissions = reddit.subreddit(subreddit).top(time_range, limit=limit)
    post_dict = {}
    for sub in submissions:
        current_ID = sub.id
        sub_dict = save_submission(sub)
        post_dict[current_ID] = sub_dict

    return post_dict

"""Gets and returns formatted CONTROVERSIAL posts (and related comments) by subreddit

    Args:
        subreddit (str): the subreddit we want to scrape posts for
        limit (int): limit to the number of submissions
        time_range (str): how far back to look for gathering data [all, day, hour, month, week, year]

    Returns:
        post_dict: a dictionary of subreddit submission dictionaries. ID as the key and relevant data stored as the value.
"""
def get_controversial_posts(subreddit, limit=None, time_range="all"):
    submissions = reddit.subreddit(subreddit).controversial(time_range, limit=limit)
    post_dict = {}
    for sub in submissions:
        current_ID = sub.id
        sub_dict = save_submission(sub)
        post_dict[current_ID] = sub_dict

    return post_dict

"""Gets and returns formatted HOT posts (and related comments) by subreddit

    Args:
        subreddit (str): the subreddit we want to scrape posts for
        limit (int): limit to the number of submissions
        time_range (str): how far back to look for gathering data [all, day, hour, month, week, year]

    Returns:
        post_dict: a dictionary of subreddit submission dictionaries. ID as the key and relevant data stored as the value.
"""
def get_hot_posts(subreddit, limit=None):
    submissions = reddit.subreddit(subreddit).hot(limit=limit)
    post_dict = {}
    for sub in submissions:
        current_ID = sub.id
        sub_dict = save_submission(sub)
        post_dict[current_ID] = sub_dict

    return post_dict

"""Gets and returns formatted RANDOM posts (and related comments) by subreddit

    Args:
        subreddit (str): the subreddit we want to scrape posts for
        limit (int): limit to the number of submissions

    Returns:
        post_dict: a dictionary of subreddit submission dictionaries. ID as the key and relevant data stored as the value.
"""
def get_random_posts(subreddit, limit=1000):
    post_dict = {}
    for i in range(limit):
        submission = reddit.subreddit(subreddit).random()
        if (submission is None):
            print('r/{} DOES NOT allow RANDOM scraping.'.format(subreddit))
            return post_dict
        sub_dict = save_submission(submission)
        current_ID = submission.id
        post_dict[current_ID] = sub_dict
        time.sleep(TIME_BETWEEN_REQUESTS)
        if (i % 100 == 0 and i > 0): print('{}/{} random posts scraped'.format(i, limit))

    return post_dict

# TODO: include functions to grab through random and search requests - will be able to give us posts not found above
# NOTE: its more difficult because random only returns one at a time so we might have to send requests
# and fill in as necessary, keeping in mind the 2 second request interval
# NOTE: search requests will be difficult too because we need to know what we're searching for
# NOTE: we have to consider this because the Reddit API no longer allows queries within a timeframe,
# plus it only allows for 1000 submissions per above queries.

def main():
    # post_dict = get_top_posts('learnpython', 1, 'month')
    # print(post_dict)
    # post_dict = get_controversial_posts('learnpython', 1)
    # print(post_dict)
    # post_dict = get_hot_posts('learnpython', 1)
    # print(post_dict)
    post_dict = get_random_posts('learnpython', 1)
    print(post_dict)

    # sub_dict = get_subreddit_meta('learnpython')
    # print(sub_dict)

if __name__ == "__main__":
    main()


import tkinter as tk

from customWidgets.NextButton import NextButton
from customWidgets.BackButton import BackButton
from customWidgets.QuitButton import QuitButton

class ThreeSiteSkinfold(tk.Frame):

   def focus_next_window(self, event):
       event.widget.tk_focusNext().focus()
       return("break")

   def focus_last_window(self, event):
       event.widget.tk_focusPrev().focus()
       return("break")

   def validateInput(self):
       validationSuccess = True
       try:
          float(self.ThreeSiteOneText.get(1.0, tk.END).strip())
          self.ThreeSiteOneText.config(bg="white")
       except:
          self.ThreeSiteOneText.config(bg="red")
          validationSuccess = False

       try:
          float(self.ThreeSiteTwoText.get(1.0, tk.END).strip())
          self.ThreeSiteTwoText.config(bg="white")
       except:
          self.ThreeSiteTwoText.config(bg="red")
          validationSuccess = False

       try:
          float(self.ThreeSiteThreeText.get(1.0, tk.END).strip())
          self.ThreeSiteThreeText.config(bg="white")
       except:
          self.ThreeSiteThreeText.config(bg="red")
          validationSuccess = False

       return validationSuccess

   def __init__(self, parent, controller):
       tk.Frame.__init__(self, parent)
       self.controller = controller

       self.ThreeSiteCalc = tk.Label(self)
       self.ThreeSiteCalc.pack()

       spacer = tk.Label(self)
       spacer.pack()

       self.centerFrame = tk.Frame(self)

       self.ThreeSiteOne = tk.Label(self.centerFrame)
       self.ThreeSiteOne.grid(row=2, column=1)

       self.ThreeSiteOneText = tk.Text(self.centerFrame)
       self.ThreeSiteOneText["height"] = 1
       self.ThreeSiteOneText["width"] = 5
       self.ThreeSiteOneText.bind("<Tab>", self.focus_next_window)
       self.ThreeSiteOneText.bind("<Shift-Tab>", self.focus_last_window)
       self.ThreeSiteOneText.grid(row=2, column=2)

       self.ThreeSiteTwo = tk.Label(self.centerFrame)
       self.ThreeSiteTwo.grid(row=2, column=3)

       self.ThreeSiteTwoText = tk.Text(self.centerFrame)
       self.ThreeSiteTwoText["height"] = 1
       self.ThreeSiteTwoText["width"] = 5
       self.ThreeSiteTwoText.bind("<Tab>", self.focus_next_window)
       self.ThreeSiteTwoText.bind("<Shift-Tab>", self.focus_last_window)
       self.ThreeSiteTwoText.grid(row=2, column=4)

       self.ThreeSiteThree = tk.Label(self.centerFrame)
       self.ThreeSiteThree.grid(row=2, column=5)

       self.ThreeSiteThreeText = tk.Text(self.centerFrame)
       self.ThreeSiteThreeText["height"] = 1
       self.ThreeSiteThreeText["width"] = 5
       self.ThreeSiteThreeText.bind("<Tab>", self.focus_next_window)
       self.ThreeSiteThreeText.bind("<Shift-Tab>", self.focus_last_window)
       self.ThreeSiteThreeText.grid(row=2, column=6)

       self.centerFrame.pack()

       spacer = tk.Label(self)
       spacer.pack()

       self.buttonFrame = tk.Frame(self)
       self.Next = NextButton(self.buttonFrame, controller, self.saveData, self.validateInput)
       self.Next.grid(row=0, column=0)

       self.Back = BackButton(self.buttonFrame, controller, self.saveData)
       self.Back.grid(row=0, column=1)

       self.Quit = QuitButton(self.buttonFrame, controller)
       self.Quit.grid(row=0, column=2)
       self.buttonFrame.pack()

   def loadData(self, person):

       if person.gender.lower() == "male":
        self.ThreeSiteCalc["text"] = "3-Site Skinfold (Male)"
        self.ThreeSiteOne["text"] = "Chest(mm): "
        self.ThreeSiteTwo["text"] = "Abdomen(mm): "
        self.ThreeSiteThree["text"] = "Thigh(mm): "

        self.ThreeSiteOneText.delete(1.0, tk.END)
        self.ThreeSiteOneText.insert(tk.END, person.threeSiteMaleChest)

        self.ThreeSiteTwoText.delete(1.0, tk.END)
        self.ThreeSiteTwoText.insert(tk.END, person.threeSiteMaleAb)

        self.ThreeSiteThreeText.delete(1.0, tk.END)
        self.ThreeSiteThreeText.insert(tk.END, person.threeSiteMaleThigh)

       else:
        self.ThreeSiteCalc["text"] = "3-Site Skinfold (Female)"
        self.ThreeSiteOne["text"] = "Suprailiac(mm): "
        self.ThreeSiteTwo["text"] = "Tricep(mm): "
        self.ThreeSiteThree["text"] = "Thigh(mm): "

        self.ThreeSiteOneText.delete(1.0, tk.END)
        self.ThreeSiteOneText.insert(tk.END, person.threeSiteFemaleSupra)

        self.ThreeSiteTwoText.delete(1.0, tk.END)
        self.ThreeSiteTwoText.insert(tk.END, person.threeSiteFemaleTricep)

        self.ThreeSiteThreeText.delete(1.0, tk.END)
        self.ThreeSiteThreeText.insert(tk.END, person.threeSiteFemaleThigh)

   def saveData(self, person):
       if person.gender.lower() == "male":
         person.threeSiteMaleChest = self.ThreeSiteOneText.get(1.0, tk.END).strip()
         person.threeSiteMaleAb = self.ThreeSiteTwoText.get(1.0, tk.END).strip()
         person.threeSiteMaleThigh = self.ThreeSiteThreeText.get(1.0, tk.END).strip()

       else:
         person.threeSiteFemaleSupra = self.ThreeSiteOneText.get(1.0, tk.END).strip()
         person.threeSiteFemaleTricep = self.ThreeSiteTwoText.get(1.0, tk.END).strip()
         person.threeSiteFemaleThigh = self.ThreeSiteThreeText.get(1.0, tk.END).strip()


__author__ = 'techbk'

import asyncio
import aiohttp

@asyncio.coroutine
def fetch_page(client, url):
    response = yield from client.get(url)
    assert response.status == 200
    #result = yield from response.read()
    text = yield from response.text()
    return text

loop = asyncio.get_event_loop()
client = aiohttp.ClientSession(loop=loop)
content = loop.run_until_complete(fetch_page(client, 'https://api.github.com/events'))
print(content)
client.close()


t = int(input())
b = int(input())
k = int(input())

if t > 100 and b <= 150:
        list = [2, 3, 4]
elif t <= 100 and b <= 150:
    if b > 30:
        list = [1, 2]
    else:
        list = [1]
elif b > 150:
    if t <= 100:
        list = [2]
    elif 100 < t <= 200:
        list = [2, 3]
    else:
        list = [0]
else:
    list = [0]

def check(l, k):
    for a in l:
        if a == k:
            return "TRUE"
    return "FALSE"

print(check(list, k))


# ---
# jupyter:
#   jupytext:
#     formats: py:percent
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.3'
#       jupytext_version: 1.14.5
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# %% [markdown]
# ## Locating and Copying Old Fluorescent Bead (FB) Measurements to a Single Folder for Testing

# %%
from pathlib import Path
import shutil
import numpy as np
import os

###############################################
# Move to project root to easily import modules
###############################################

try:  # avoid changes if already set
    print("Working from: ", PROJECT_ROOT)
except NameError:
    try:  # running from Spyder
        PROJECT_ROOT = Path(__file__).resolve()
    except NameError:  # running as Jupyter Notebook
        PROJECT_ROOT = Path(os.getcwd()).resolve().parent.parent
    os.chdir(PROJECT_ROOT)
    print("Working from: ", PROJECT_ROOT)

from utilities.display import Plotter
from utilities.file_utilities import (
    load_object,
    save_object,
)

# define main data directory
DATA_ROOT = Path("D:\OneDrive - post.bgu.ac.il\gSTED_sFCS_Data/")

# %% [markdown]
# List all paths for files in directories named "FB" in the main folder (`DATA_ROOT`)

# %%
OLD_DATA_ROOT = Path("D:\old\Google Drive\Dotan's old drive\Dotan TDC Measurments")

fb_paths = [path for path in OLD_DATA_ROOT.rglob("*.mat") if "fb" in str(path).lower()]

# TEST
print(f"Found {len(fb_paths)} files:\n")
print("\n".join([str(file_path) for file_path in fb_paths]))

# %% [markdown]
# Copy those files to a 'regular' date directory

# %%
# create the folder if necessary
destination_dir_path = DATA_ROOT / "24_05_2023" / "image"
destination_dir_path.mkdir(parents=True, exist_ok=True)

# # copy the files to new directory
for file_path in fb_paths:
    shutil.copy(file_path, destination_dir_path)

data_analysis/example_scripts/Getting all old fluorescent bead measurements from main folder.py


import random

start = int(input('请决定数字范围开始值：'))
end = int(input('请决定数字范围结束值：'))
r = random.randint(start,end)
count = 0

while True:
	count += 1
	num = int(input('请猜一个数字'))
	if num == r:
		print('终于猜对了！')
		print('这是你猜的第 {} 次'.format(count))
		break
	elif num > r:
		print('猜小一点')
	elif num < r:
		print('猜大一点')
	print('这是你猜的第 {} 次'.format(count))


from datetime import datetime
from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.sensors.external_task import ExternalTaskSensor

with DAG(dag_id="7.2-externalTaskSensor",
         description="DAG Secundario",
         schedule_interval="@daily",
         start_date=datetime(2022, 5, 1),
         end_date=datetime(2022, 8, 1),
         max_active_runs=1) as dag:

    t1 = ExternalTaskSensor(task_id="waiting_dag",
                            external_dag_id="7.1-externalTaskSensor",
                            external_task_id="tarea1",
                            poke_interval=10)

    t2 = BashOperator(task_id="tarea2",
                      bash_command="sleep 10 && echo 'DAG 2 finalizado!'",
                      depends_on_past=True)

    t1 >> t2