# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal
data65 = np.genfromtxt('18_07_16_Kroner_Keppler/Laserscan.txt',skip_header = 1,unpack=True)
# data65 = np.transpose(data65)
dy = 1
dx = 1
y, x = np.mgrid[slice(0.5, 127.5 + dy, dy),
                slice(0, 35 + dx, dx)]
cmap = plt.get_cmap('Greys')
plt.pcolormesh(x,y,data65,cmap=cmap,label='ADC Counts')
plt.colorbar(label='ADC Counts')
plt.xlabel(r'Relative Position / 10 μm')
plt.ylabel(u'Streifen')
plt.legend(loc="best")
plt.ylim(70,76)
plt.grid()
plt.tight_layout()
plt.savefig('build/6.5/Plot_65c.pdf')
plt.close()

# print (data65[0]) # Streifen Nummero 1
Position = range(0,35)
copy_data65 = list(data65[72])
pos_maximum1_72 = copy_data65.index(max(copy_data65))
copy_data65[pos_maximum1_72]=0
pos_maximum2_72 = copy_data65.index(max(copy_data65))
# print('Pitch 72= %f'%)

copy_data65=[]
copy_data65 = list(data65[73])
pos_maximum1_73 = copy_data65.index(max(copy_data65))
copy_data65[pos_maximum1_73]= 0
pos_maximum2_73 = copy_data65.index(max(copy_data65))
# peakind = signal.find_peaks_cwt(data65[72],np.arange(1,10))
# print (peakind)
# print('Pitch 73= %f'%)

Periodizita = abs((pos_maximum2_72-pos_maximum1_73)*10)
Periodizitb = abs((pos_maximum2_73-pos_maximum1_72)*10)
# print(Periodizita,Periodizitb)

offset=10
for i in range(pos_maximum1_73,35):
    # print(data65[73][i])
    if data65[73][i]<offset:
        pos_keinSignal_73 =i
        break
breite_laser_73 = pos_keinSignal_73 - pos_maximum1_73
# print('breite_laser_73',breite_laser_73)
for k in range(pos_maximum2_72,35):
    # print(data65[72][k])
    if data65[72][k]<offset:
        pos_keinSignal_72 =k
        break
breite_laser_72 = pos_keinSignal_72 - pos_maximum2_72
# print('breite_laser_72',breite_laser_72)

for i in range(pos_keinSignal_73,35):
    if data65[73][i]>offset:
        pos_wiederSignal_73 =i
        break
breite_metall_73 = pos_wiederSignal_73 - pos_keinSignal_73
# print('breite_metall_73',breite_metall_73)
for i in range(pos_keinSignal_72,35):
    if data65[72][i]>offset:
        pos_wiederSignal_72 =i
        break
breite_metall_72 = pos_wiederSignal_72 - pos_keinSignal_72
# print('breite_metall_72',breite_metall_72)

plt.plot(Position,data65[72],'-x',label = 'Streifen 72')
plt.plot(Position,data65[73],'-o',label = 'Streifen 73')
plt.hlines(max(copy_data65)-10,pos_maximum1_73,pos_maximum2_72,label=u'Periodizität')
plt.hlines(max(copy_data65)-20,pos_maximum2_73,pos_maximum1_72)
plt.hlines(min(copy_data65)+offset,pos_keinSignal_73,pos_wiederSignal_73,'r',label=u'Metallbreite')
plt.hlines(min(copy_data65)+offset,pos_keinSignal_72,pos_wiederSignal_72,'r')
plt.hlines(max(copy_data65),pos_maximum1_73,pos_keinSignal_73,'g',label=u'Laserbreite')
plt.hlines(max(copy_data65),pos_maximum2_72,pos_keinSignal_72,'g')
plt.xlabel(r'Relative Position / 10 μm')
plt.ylabel(u'ADC Counts')
plt.legend(loc="best")
plt.tight_layout()
plt.savefig('build/6.5/Plot_65cb.pdf')
plt.close()

print('\n')

print('Relevante Streifen: 72, 73')
print('Ausdehnung des Lasers = %f um'%np.mean([breite_laser_72,breite_laser_72]*10) )
print('Periodizität = %f um'%np.mean([Periodizita,Periodizitb]))
print('Metallbreite = %f um'%np.mean([breite_metall_72,breite_metall_73]))
print('\n')

# print(sum(range(0,35))/35)
# x = range(0,50)
# a = np.mean(x)
# b = np.std(x)
# print(a,b)
# data65_ohne_rauschen = np.array()
# relevanter_streifen = np.array()
# for i in range(0,len(data65)):
#     for j in range(0,len(data65[i])):
#         if data65[i][j] > 1:
#             data65_ohne_rauschen.append(data65[i][j])
#             relevanter_streifen.append([i])
#
# print(len(relevanter_streifen), len(data65_ohne_rauschen))
# data = np.concatenate((relevanter_streifen, data65_ohne_rauschen))
# print(data)
# position = []
# beginn = 5.5
# for idx in range(0,len(data65)):
#     position.append(beginn)
#     beginn += 0.01
#
#
#
# x = range(0,128)
# plt.plot(x, data65, 'x', label='data')
# plt.ylabel(r'ADC Daten')
# plt.xlabel(r'Postion')
# plt.legend(loc="best")
# plt.savefig('build/6.5/Plot_65.pdf')
# plt.close()

# Kein plan
# ich will ADC über x Position plotten, aber nur von channel 60-80
# ich will mir anzeigen lassen welcher channel welches Signal misst.
# was ist ein pitch
# welche Streifennummer


#!/usr/bin/env python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import pytest

from mtconnect.connect import Connect

pytest_plugins = 'pytester'

def pytest_funcarg__testmoztrap(request):
    return TestSetup(request)

def pytest_addoption(parser):
    # required
    group = parser.getgroup('test-moztrap', 'test-moztrap')
    group._addoption('--test-mt-username',
                     action='store',
                     dest='test_moztrap_username',
                     metavar='str',
                     help='moztrap username')
    group._addoption('--test-mt-url',
                     action='store',
                     dest='test_moztrap_url',
                     default='moztrap.allizom.org',
                     metavar='str',
                     help='test run name')
    group._addoption('--test-mt-apikey',
                     action='store',
                     dest='test_moztrap_apikey',
                     metavar='str',
                     help="Ask your MozTrap admin to generate an API key "
                     "in the Core / ApiKeys table and provide it to you.")

def pytest_sessionstart(session):
    config = session.config

    if not config.option.test_moztrap_apikey and \
           config.option.test_moztrap_username:

        raise pytest.UsageError("--test-mt-username and --test-mt-apikey "
        "are required.")

def pytest_runtest_setup(item):
    TestSetup.username = item.config.option.test_moztrap_username
    TestSetup.apikey = item.config.option.test_moztrap_apikey
    TestSetup.url = item.config.option.test_moztrap_url

    TestSetup.connect = Connect(
        "https",
        TestSetup.url,
        TestSetup.username,
        TestSetup.apikey,
        )

class TestSetup:
    '''
        This class is just used for monkey patching
    '''
    def __init__(self, request):
        self.request = request


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import time

output_path='/home/guohf/AI_tutorial/ch8/output_files/'
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Dataloader():
    def __init__(self):
        #path= tf.keras.utils.get_file('nietzsche.txt',
        #                             origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt')
        path = '/home/guohf/old_man_and_sea.txt'
        with open(path, encoding='utf-8') as f:
            self.raw_text= f.read().lower()
        self.chars= sorted(list(set(self.raw_text)))
        self.char_indices= dict((c,i) for i, c in enumerate(self.chars))
        self.indices_char= dict((i,c) for i, c in enumerate(self.chars))
        self.text= [self.char_indices[c] for c in self.raw_text]

    def get_batch(self, batch_size):
        first_word= []
        next_word= []
        for i in range(batch_size):
            index= np.random.randint(0, len(self.text)-3-batch_size)
            first_word.append(self.text[index:index+3])
            next_word.append(self.text[index+3])
        return np.array(first_word), np.array(next_word)

class NN(nn.Module):
    def __init__(self, num_chars, batch_size, seq_size):
        super(NN, self).__init__()
        self.num_chars= num_chars
        self.batch_size= batch_size
        self.seq_size= seq_size
        self.embedding= nn.Embedding(self.num_chars, 100)
        self.fc1 = nn.Sequential(
            nn.Linear(self.seq_size*self.num_chars*100, 250),
            nn.ReLU()
        )
        self.fc2 = nn.Linear(250, self.num_chars)

    # 定义前向传播过程，输入为inputs
    def forward(self, inputs):
        inputs= torch.LongTensor(inputs)
        inputs= torch.reshape(inputs,(self.batch_size*self.seq_size, 1))
        inputs= torch.zeros(self.batch_size*self.seq_size, self.num_chars).scatter_(1, inputs, 1) # one-hot encoding
        inputs= torch.reshape(inputs, (self.batch_size, self.seq_size, self.num_chars))
        x= self.embedding(inputs.long())
        x= x.view(x.size(0), -1)
        x = self.fc1(x)
        outputs = self.fc2(x)
        return outputs

    def predict(self, inputs, temperature=1.):
        inputs = torch.LongTensor(inputs)
        inputs = torch.reshape(inputs, (self.seq_size, 1))
        inputs = torch.zeros(self.seq_size, self.num_chars).scatter_(1, inputs, 1)  # one-hot encoding
        inputs = torch.reshape(inputs, (1, self.seq_size, self.num_chars))
        x = self.embedding(inputs.long())
        x = x.view(x.size(0), -1)
        x = self.fc1(x)
        predicts = self.fc2(x)
        prob = F.softmax(predicts/ temperature).detach().numpy()
        return np.array([np.random.choice(self.num_chars, p=prob[0, :])])

dataloader = Dataloader()
batch_size= 50
seq_size= 3
learning_rate= 0.001
EPOCH= 5

net = NN(len(dataloader.chars), batch_size, seq_size).to(device)
criterion = nn.CrossEntropyLoss()  # 交叉熵损失函数，通常用于多分类问题上
optimizer= optim.Adam(net.parameters(), lr= learning_rate)

# 训练
if __name__ == "__main__":
    start_time= time.time()
    for epoch in range(EPOCH):
        sum_loss = 0.0
        # 数据读取
        inputs, labels = dataloader.get_batch(batch_size)

        # 梯度清零
        optimizer.zero_grad()

        # forward + backward
        outputs = net(inputs)
        loss = criterion(outputs, torch.LongTensor(labels))
        loss.backward()
        optimizer.step()
        end_time= time.time()
        print('the %d epoch loss: %.03f, total running time: %.2f s'% (epoch, loss.item(),end_time-start_time))

    #torch.save(net.state_dict(),'/home/guohf/AI_tutorial/ch8/model/oldman_wordbased_%d.pt'%EPOCH)
    fout = open(output_path + str(EPOCH) + "_word_based_output.txt", "w")

    X_, _=dataloader.get_batch(1)
    for diversity in [0.2,0.5,1.0,1.2]:
        X=X_
        print("diversity %f:" % diversity)
        fout.write("diversity %f:\n" % diversity)
        for t in range(100):
            y_pred = net.predict(X,diversity)
            print(dataloader.indices_char[y_pred[0]], end='',flush=False)
            print("shape of X:", np.shape(X))
            X=np.concatenate([X[:,1:], np.expand_dims(y_pred, axis=1)], axis=-1)
        print("/n")
        fout.write("\n")
    fout.close()


f = open('c://test.txt','w')
f.write('hello world')
f.close()

f = open('c://test.txt','r')
print(f.read())
f.close()

#按字节处理
with open(r'c:\test.txt') as file:
    while True:
        char = file.read(1)
        if not char: break
        print('按字节处理:',char)
#按行处理
with open(r'c:\test.txt') as file:
    while True:
        line = file.readline()
        if not line: break
        print('按行处理:',line)
#读取所有内容，用read迭代每个字符
with open(r'c:\test.txt') as file:
    while True:
        char = file.read()
        if not char: break
        print('读取所有内容:',char)
#读取所有内容，用readlines迭代行(可能会占用太多内存)
with open(r'c:\test.txt') as file:
    while True:
        line = file.readlines()
        if not line: break
        print('读取所有内容:',line)

#使用fileinput实现懒惰行迭代
import fileinput
for line in fileinput.input(r'c:\test.txt'):
    print('使用fileinput实现懒惰行迭代',line)

#迭代文件
f = open(r'c:\test.txt')
for line in f:
    print('迭代文件:',line)
f.close()

#迭代标准输入所有行
import sys
for line in sys.stdin:
    print('print sys.stdin:',line)


#-------------------------------------------------------------------------------
#
# Project: EOxServer <http://eoxserver.org>
# Authors: Martin Paces <martin.paces@eox.at>
#
#-------------------------------------------------------------------------------
# Copyright (C) 2014 EOX IT Services GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies of this Software or works derived from this Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#-------------------------------------------------------------------------------

import sys
#import traceback
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from eoxserver.id2path.models import TrackedObject as TO
from eoxserver.id2path.models import PathItem as PI
from eoxserver.resources.coverages.management.commands import CommandOutputMixIn

# type-string to code conversion
STR2TYPE = dict((b, a) for (a, b) in PI.TYPE_CHOICES)

class Command(CommandOutputMixIn, BaseCommand):
    option_list = BaseCommand.option_list + (
#        make_option('-i','--id','--identifier',
#            dest='identifiers',
#            action="callback", callback=_variable_args_cb,
#            default=None,
#            help=("Optional. Identifiers of the tracked objects to be removed.")
#        ),
#        make_option('-p','--path'
#            dest='paths',
#            action="callback", callback=_variable_args_cb,
#            default=None,
#            help=("Optional. Path Items to be removed.")
#        ),
        make_option('-i', '--id', '--identifier',
            dest='identifier',
            action='store', type='string',
            default=None,
            help=("Optional. Identifier for which the path is removed.")
        ),
        make_option('--remove-empty',
            dest='remove_empty',
            action='store_true',
            default=False,
            help=("Optional. Force removal of empty objects. By default, the"
                  " tracked object remain registered.")
        ),
    )

    args = "[-i <identifier>]"

    help = (
    """
    This command deletes 'id2path' path items and/or tracked objects. The input
    is expected to be the same as the full output of the 'list' command:

      - any line starting by the hash character resets the identfier;
      - any other line is expected to be the collon separated path record.
      - empty lines are ignored

    Sample:

    #<identifier no.1>
    <path 1.1>;<type 1.1>;<label 1.1>
    <path 1.2>;<type 1.2>;<label 1.2>
    #<identifier no.2>
    <path 2.1>;<type 2.1>;<label 2.1>
    <path 2.2>;<type 2.2>;<label 2.2>

    The types and labels are ignored and can be omited. The use of the object
    identifiers is optional but recomended in cases when one path is used
    mutiple times. If the tracked object is not specified all path items
    from multiple objects are removed.
    """
    )
    #--------------------------------------------------------------------------

    def handle(self, *args, **options):
        # flags
        self.to_invalid = False

        # statistic
        self.to_found = 0
        self.to_failure = 0
        self.to_remove = 0
        self.to_rfailure = 0
        self.pi_found = 0
        self.pi_remove = 0
        self.pi_unlink = 0
        self.pi_abort = 0
        self.pi_failure = 0

        #----------------------------------------------------------------------

        def _get_object(identifier):
            """ get an existing tracked object """
            self.to_invalid = False
            if identifier is None:
                return None
            try:
                tobj = TO.objects.get(identifier=identifier)
            except TO.DoesNotExist:
                tobj = None
                self.to_invalid = True
                self.to_failure += 1
                self.print_err("Tracked object not found! Subsequent path "
                         "items' removals will be aborted! ID='%s'"%identifier)
            else:
                self.to_found += 1
                self.print_msg("An existing tracked object found. "
                                                     "ID='%s'"%tobj.identifier)
            return tobj

        def _remove_object_if_empty(tobj):
            """ remove tracked object if it has no path items """
            if tobj is None:
                return
            if tobj.paths.count() > 0:
                return
            try:
                with transaction.commit_on_success():
                    tobj.delete()
            except Exception as ex:
                self.to_rfailure += 1
                self.print_err("Tracked object removal failed! ID='%s'"
                          " REASON=%s: %s"%(tobj.identifier, type(ex), str(ex)))
            else:
                self.to_remove += 1
                self.print_msg("Tracked object removed. ID='%s'"
                                                          ""%tobj.identifier)

        def _remove_path(tobj, path):
            """ remove path item """
            # check the abort flag
            if self.to_invalid:
                self.pi_abort += 1
                self.print_wrn("Path item removal aborted! PATH='%s'"%path)
                return

            if tobj is None:
                # case 1 - no tracked object given - the path items will be removed
                try:
                    pitm = PI.objects.get(path=path)
                except PI.DoesNotExist:
                    self.pi_failure += 1
                    self.print_err("Path item not found! PATH='%s'"%path)
                    return
                else:
                    self.pi_found += 1

            else:
                # case 2 - tracked object given - relation to the tracked object
                #          will be removed and the path item will be removed if
                #          relation has left
                try:
                    pitm = tobj.paths.get(path=path)
                except PI.DoesNotExist:
                    self.pi_failure += 1
                    self.print_err("Path item not found for the given tracked"
                          " object! ID='%s' PATH='%s'"%(tobj.identifier, path))
                    return
                else:
                    self.pi_found += 1

                if pitm.owners.exclude(id=tobj.id).count() > 0:
                    # path item has multiple owners - not to be removed!
                    try:
                        # unlink the path object relation relation
                        with transaction.commit_on_success():
                            pitm.owners.remove(tobj)
                    except Exception as ex:
                        self.pi_failure += 1
                        self.print_err("Path item unlinking failed! ID='%s'"
                            " PATH='%s' REASON=%s: %s"%(tobj.identifier, path,
                                                            type(ex), str(ex)))
                    else:
                        self.pi_unlink += 1
                        self.print_msg("Path item unlinked from the tracked"
                          " object. ID='%s' PATH='%s'"%(tobj.identifier, path))
                    return

            # path has only one owner - shall be removed
            try:
                with transaction.commit_on_success():
                    pitm.delete()
            except Exception as ex:
                self.pi_failure += 1
                self.print_err("Path item removal failed! PATH='%s'"
                                " REASON=%s: %s"%(path, type(ex), str(ex)))
            else:
                self.pi_remove += 1
                self.print_msg("Path item removed PATH='%s'"%path)

        #----------------------------------------------------------------------

        # get input parameters
        identifier = options.get('identifier', None)
        remove_empty = options.get('remove_empty', False)

        # get the tracked object for the given identifier
        tobj = _get_object(identifier)

        # process the input stream
        for line_num, line in enumerate(sys.stdin, 1):
            line = line.strip()
            if len(line) == 0: # skip empty lines
                continue

            # object identifier
            if line[0] == '#':
                identifier = line[1:]
                if remove_empty:
                    _remove_object_if_empty(tobj)
                tobj = _get_object(identifier)
                continue

            # parse the line
            tmp = line.split(";")
            path = tmp[0] # NOTE: anything else than the path is ignored
            _remove_path(tobj, path)

        if remove_empty:
            _remove_object_if_empty(tobj)

        # print final statistics
        to_total = self.to_found + self.to_failure
        pi_total = self.pi_remove + self.pi_unlink + self.pi_abort + self.pi_failure
        self.print_msg("Tracked Objects removed:  %d of %d"%(self.to_remove, to_total))
        self.print_msg("Tracked Objects failed:   %d of %d"%(self.to_failure, to_total))
        self.print_msg("Path Items removed:       %d of %d"%(self.pi_remove, pi_total))
        self.print_msg("Path Items unlinked:      %d of %d"%(self.pi_unlink, pi_total))
        self.print_msg("Path Items aborted:       %d of %d"%(self.pi_abort, pi_total))
        self.print_msg("Path Items failed:        %d of %d"%(self.pi_failure, pi_total))

        if (self.to_failure + self.pi_abort + self.pi_failure) > 0:
            raise CommandError("Errors encountered!")


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
import time
from selenium.webdriver.support.ui import Select
class Search2(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver=webdriver.Firefox(executable_path=r"C:\FireFox\geckodriver.exe")
        cls.driver.get("http://automationpractice.com/index.php")
        cls.driver.maximize_window()
        time.sleep(3)
    def test_element(self):
        self.driver.find_element_by_xpath('//*[@id="search_query_top"]').click()
        self.driver.find_element_by_xpath('//*[@id="search_query_top"]').send_keys("Dress")
        time.sleep(5)
        #if (self.driver.find_element_by_class_name('search_query')).text == "Dress":
            #print ("True")
        #else:
            #print("False")

        lst = self.driver.find_elements_by_xpath('//*[@id="index"]/div[2]/ul/li')
        for elm in lst:
            elm_text = elm.text
            self.assertIn("Dress", elm_text, 'False')
    def tearDown(cls):
        cls.driver.quit()
if __name__ == '__main__':
    unittest.main()


import json
import logging
import http.client

logger = logging.getLogger(__name__)

HTTP_STATUSES_STRINGS = http.client.responses

class Headers:
    def __init__(self, env=None):
        if isinstance(env, Headers):
            self._headers = env._headers[:]
        elif isinstance(env, dict):
            self._headers = [(key, val) for key, val in env.items()]
        elif isinstance(env, (list, tuple)):
            self._headers = [(key.replace('HTTP_', '').replace('_', '-'), subval.strip())
                             for key, val in env
                             for subval in val.split(',')]
        else:
            self._headers = []

    def __getitem__(self, item: str):
        for name, val in self._headers:
            if name.lower() == item.lower():
                return val
        raise KeyError

    def get(self, item: str, default=None) -> str:
        try:
            return self[item]
        except KeyError:
            return default

    def getall(self, item: str) -> list:
        return [val for name, val in self._headers if name.lower() == item.lower()]

    def add(self, name: str, val: str):
        self._headers.append((name, val))

    def __contains__(self, item):
        try:
            self[item]
            return True
        except KeyError:
            return False

    def __repr__(self):
        return 'Headers({})'.format(', '.join(['{}: "{}"'.format(key, val) for key, val in self._headers]))

    __str__ = __repr__

    def __eq__(self, other):
        if not isinstance(other, Headers):
            return False

        return sorted(self._headers) == sorted(other._headers)

    @property
    def wsgi_headers(self):
        return self._headers

    @classmethod
    def from_wsgi_env(cls, env: dict) -> 'Headers':
        h = cls()
        h._headers = [(key.replace('HTTP_', '').replace('_', '-'), subval.strip())
                      for key, val in env.items() if key.startswith('HTTP')
                      for subval in val.split(',')]
        return h

class Cookies(dict):
    def __init__(self, env=None):
        if isinstance(env, Cookies):
            super().__init__(env)
        elif not env:
            super().__init__()
        elif isinstance(env, str):
            self._init(env.split(';'))
        else:
            self._init(env)

    def _init(self, item):
        if isinstance(item, dict) and 'HTTP_COOKIE' in item:
            for i in item['HTTP_COOKIE'].split(';'):
                self._init(i.strip())

        elif isinstance(item, dict):
            self.update(item)

        elif isinstance(item, (list, tuple)):
            for i in item:
                self._init(i)

        elif isinstance(item, str):
            key, val = item.split('=')
            self[key] = val

    def set(self, key: str, val: str):
        self[key] = val

    @property
    def wsgi_header_value(self):
        return '; '.join(['{}={}'.format(key, val) for key, val in self.items()])

    @property
    def wsgi_header(self):
        return 'HTTP_COOKIE', self.wsgi_header_value

class Request:
    _content = None
    _headers = None
    _cookies = None

    def __init__(self, environment):
        self.env = environment

    @property
    def path(self):
        return self.env['PATH_INFO']

    @property
    def method(self):
        return self.env['REQUEST_METHOD']

    @property
    def content(self):
        """
        :return: raw byte body content of request
        """
        if self._content is None:
            self._content = self.env['wsgi.input'].read(int(self.headers.get('content-length')) or 0)

        return self._content

    @property
    def text(self):
        """
        :return: decoded string body content of request
        """
        return self.content.decode('utf-8')

    @property
    def json(self):
        return json.loads(self.text)

    @property
    def headers(self):
        if self._headers is None:
            self._headers = Headers.from_wsgi_env(self.env)

        return self._headers

    @property
    def app(self):
        return self.env['app']

    @property
    def args(self):
        logger.debug('Querry url params: "{}"'.format(self.env['QUERY_STRING']))
        return dict(pair.split('=') for pair in self.env['QUERY_STRING'].split('&'))

    @property
    def cookies(self):
        if self._cookies is None:
            self._cookies = Cookies(self.env)

        return self._cookies

class Response:
    default_status = 200

    def __init__(self, response=None, headers=None, status=default_status, cookies=None):
        if isinstance(headers, Headers):
            self.headers = headers
        else:
            self.headers = Headers(headers)

        if response is None:
            self.response = []
        elif isinstance(response, str):
            self.response = [response.strip().encode() + b'\n']
            self.headers.add('Content-Type', 'text/plain')
            self.headers.add('Content-length', str(self.content_length))
        elif isinstance(response, (list, dict)):
            self.response = [json.dumps(response).encode()]
            self.headers.add('Content-Type', 'application/json')
            self.headers.add('Content-length', str(self.content_length))
        else:
            self.response = response

        self.status = status

        if isinstance(cookies, Cookies):
            self.cookies = cookies
        else:
            self.cookies = Cookies(cookies)

    def make_status_str(self):
        return '{} {}'.format(self.status, HTTP_STATUSES_STRINGS[self.status])

    def make_headers(self):
        if self.cookies.wsgi_header:
            return self.headers.wsgi_headers + [self.cookies.wsgi_header]
        return self.headers.wsgi_headers

    def __call__(self, environment, start_response):
        status = self.make_status_str()

        start_response(status, self.make_headers())
        logger.debug('[Response] returning data: "{}"'.format(self.response))
        return self.response

    @property
    def content_length(self):
        return sum(len(x) for x in self.response)


import requests
import re
import csv

def strip_proto(url):
	if "http" in url:		#REMOVE HTTP IF IT EXISTS
		url1 = url[7:]
	if "https" in url:		#REMOVE HTTPS IF IT EXISTS
		url1 = url[8:]
	if "www." in url1:		#REMOVE WWW IF IT EXISTS
		url1 = url1[4:]
	return url1

def start_spider(website):
	headers = {"user-agent": "Mozilla/5.0"}	#HEADERS GET REQ PAST FIREWALLS
	r = requests.get('{}'.format(website), headers = headers)
	content = r.text
	urls = re.findall('href="*.+?"', content)	#REGEX SEARCH FOR URLS
	return urls

def fix_proto(sites):
	full_url = []
	for site in sites:
		site = site[6:-1] #REMOVE HREF AND LAST "
		if "http" not in site:
			if (len(site) > 0) and (site[0] != "/"):
				site = "/" + site
			if (len(site) > 1) and (site[1] != "/"):
				site = "http://www." + domain1 + site
				full_url.append(site)
			if (len(site) > 1) and (site[1] == "/"):
				site = "http:" + site
				full_url.append(site)
		else:
			full_url.append(site)
	return full_url

def remove_slash(with_slashes):
	no_slash = []
	for s in with_slashes:
		slash_len = len(s)
		if s[slash_len-1] == "/":
			s = s[:-1]
		no_slash.append(s)
	return no_slash

def remove_dups(dups):
	no_dup = []
	for not_dup in dups:
		if not_dup not in no_dup:
			no_dup.append(not_dup)
	return(no_dup)

def remove_anchors(has_anchors):
	no_anchors = []
	for anchor in has_anchors:
		if ("#" not in anchor):
			no_anchors.append(anchor)
	return no_anchors

def remove_outdomain(all_domains, my_domain):
	my_domains = []
	for the_domain in all_domains:
		if my_domain in the_domain:
			my_domains.append(the_domain)
	return my_domains

######################################################################################

domain = input("What domain do you want to spider? Include the protocol and make sure you have the correct address, please: ")

domain1 = strip_proto(domain)
all_urls = start_spider(domain)
full_urls = fix_proto(all_urls) #THIS WILL PUT THE URL IN CORRECT FORMAT FOR REQUESTS MODULE
noslash_urls = remove_slash(full_urls) #THIS HELPS PREVENT DUPLICATE URLS
nodup_urls = remove_dups(noslash_urls) #THIS REMOVES DUPLICATE URLS
noanchor_urls = remove_anchors(nodup_urls) #THIS REMOVES THE ANCHOR URLS
my_urls = remove_outdomain(noanchor_urls, domain1) #REMOVES URLS THAT ARE OUTSIDE THE DOMAIN

counter = 0
for x in my_urls:
	counter += 1
	print("{}.".format(counter), x)

#THIS ADDS THE URL LIST TO A MASTERLIST
masterlist = my_urls
list_length = len(masterlist)	#ASSIGNS LENGTH OF MASTER LIST TO LIST_LENGTH

print("##################################################################")
print("The above URLs are the URLs found on the first page of the domain")
print("There are {} URLs".format(list_length))
input("Hit enter to spider each URL.")

#THIS SPIDERS THE URLS IN THE MASTERLIST. SAME PROCESS AS ABOVE
list_count = 0
templist = []
for parent in masterlist:
	list_count += 1
	print ("Testing {}.".format(list_count), parent, "...")
	print("We're {0:.2f}% done...".format(list_count/list_length))
	kids = start_spider(parent)
	kids1 = fix_proto(kids)
	kids2 = remove_slash(kids1)
	kids3 = remove_dups(kids2)
	kids4 = remove_outdomain(kids3, domain1)
	kids5 = remove_anchors(kids4)
	for x6 in kids5:			#THIS FOR STATEMENT INSERTS THE UNIQUE URLS INTO A TEMPORARY LIST
		if x6 not in masterlist:
			templist.append(x6)

masterlist = masterlist + templist	#THIS COMBINES TEMPLIST & MASTERLIST
list_length = len(masterlist)

#THIS WRITES THE MASTERLIST TO A CSV
with open("masterfile.csv", "w") as myfile:
    	wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    	wr.writerow(masterlist)

input("The masterlist now contains {} URLs. Do you want to spider all those entries?".format(list_length))
print("No!")


#!/usr/local/bin/python2.7
##
# OOIPLACEHOLDER
#
# Copyright 2014 Raytheon Co.
##
import os

from mi.logging import config
from mi.dataset.dataset_parser import DataSetDriverConfigKeys
from mi.dataset.driver.cg_stc_eng.stc.mopak_o_dcl_common_driver import MopakDriver
from mi.dataset.parser.mopak_o_dcl import \
    MopakODclAccelParserRecoveredDataParticle, \
    MopakODclRateParserRecoveredDataParticle, \
    MopakParticleClassType

def parse(basePythonCodePath, sourceFilePath, particleDataHdlrObj):

    config.add_configuration(os.path.join(basePythonCodePath, 'res', 'config', 'mi-logging.yml'))

    parser_config = {
        DataSetDriverConfigKeys.PARTICLE_MODULE: 'mi.dataset.parser.mopak_o_dcl',
        DataSetDriverConfigKeys.PARTICLE_CLASS: None,
        # particle_class configuration does nothing for multi-particle parsers
        # put the class names in specific config parameters so the parser can get them
        # use real classes as objects instead of strings to make it easier
        DataSetDriverConfigKeys.PARTICLE_CLASSES_DICT:
            {MopakParticleClassType.ACCEL_PARTCICLE_CLASS: MopakODclAccelParserRecoveredDataParticle,
             MopakParticleClassType.RATE_PARTICLE_CLASS: MopakODclRateParserRecoveredDataParticle}
    }

    driver = MopakDriver(sourceFilePath, particleDataHdlrObj, parser_config)

    return driver.process()


#bl_info = {
#    "name": "Extension Add",
#    "author": "marvin.k.breuer",
#    "version": (0, 1, 0),
#    "blender": (2, 72, 0),
#    "location": "View3D > Toolbar",
#    "warning": "",
#    "description": "Toolkit Extension",
#    "wiki_url": "",
#    "category": "User Panel",
#}

import bpy
from bpy import *
# property group containing all properties for the gui in the panel
#Dropdown Arrow ### general display properties = arrow tooltips
class DropdownMetaTool_UV_Props(bpy.types.PropertyGroup):
    """
    Fake module like class
    bpy.context.window_manager.metawindowtool_uv
    """
    ### UV Unwrap Tools
    display_tab_unwrap = bpy.props.BoolProperty(name = "UV Tools", description = "Display UV Tools", default = True)
    display_tab_uvut = bpy.props.BoolProperty(name = "UV Utility", description = "Display UV Utility Tools", default = False)
    display_tab_uvsure = bpy.props.BoolProperty(name = "SureUVW", description = "Display SureUVW Tools", default = False)
    display_tab_unwrapset = bpy.props.BoolProperty(name = "UV Unwrap", description = "Display UV Unwrap Tools", default = False)
    display_tab_uvnext = bpy.props.BoolProperty(name = "TextureSpace / Freestyle", description = "Display TextureSpace / Freestyle Tools", default = False)

#############################################################
###----------------  UV Mapping  -------------------------###
###----------------  UV Mapping  -------------------------###
#############################################################

# Sub Location
class SubLoc_UVS():
    """UV Tools"""
    bl_category="META"
    #bl_region_type = 'TOOLS'
    bl_region_type = 'UI'
    bl_space_type = 'VIEW_3D'

    @classmethod
    def poll(cls, context):
        return context.scene.osc_uvs

## Sub Panel
class META_TAB_UVS(SubLoc_UVS, bpy.types.Panel):
    """UV Tool"""
    bl_idname = "meta.uvtools"
    bl_label = "[UV UNWRAP]"

    def draw(self, context):
        lt = context.window_manager.metawindowtool_uv
        wm = context.window_manager
        scn = context.scene
        obj = context.object
        scene = context.scene
        layout = self.layout

############################-------------------------------------------------------
#######  UV Mapping  #######-------------------------------------------------------
#######  UV Mapping  #######-------------------------------------------------------
############################-------------------------------------------------------

### Objectmode ###

        col = layout.column(align=True)

        obj = context
        if obj and obj.mode == 'OBJECT':

            if lt.display_tab_unwrap:
                box = layout.box()
                row = box.row()
                row.prop(lt, "display_tab_unwrap", text="", icon='TRIA_DOWN')
            else:
                box = layout.box()
                row = box.row()
                row.prop(lt, "display_tab_unwrap", text="", icon='TRIA_RIGHT')

            row.label("UvTools...")

            if lt.display_tab_unwrap:

                col = layout.column(align=True)
                box = col.column(align=True).box().column()
                col_top = box.column(align=True)

                row = col_top.row(align=True)
                row.operator("transform.translate", text ="Move Texture Space").texture_space = True

                row = col_top.row(align=True)
                row.operator("uv.uv_equalize" , text ="UV Equalize")

######  UV Utility  ######-------------------------------------------------
######  UV Utility  ######-------------------------------------------------

                if lt.display_tab_uvut:
                    box = layout.box()
                    row = box.row()
                    row.prop(lt, "display_tab_uvut", text="", icon='DISCLOSURE_TRI_DOWN_VEC')
                else:
                    box = layout.box()
                    row = box.row()
                    row.prop(lt, "display_tab_uvut", text="", icon='DISCLOSURE_TRI_RIGHT_VEC')

                row.label("UV Utility...")

                if lt.display_tab_uvut:

                    col = layout.column(align=True)
                    box = col.column(align=True).box().column()
                    col_top = box.column(align=True)

                    row = col_top.row(align=True)
                    row.operator("uvutil.change_index", text="Drop Active UV Back")

                    col_top = box.column(align=True)
                    row = col_top.row(align=True)
                    row.prop(scene, "UVTexRenderActive")

                    col_top = box.column(align=True)
                    row = col_top.row(align=True)
                    row.operator("uvutil.select_index", text="Select UVTexCoord")
                    row = col_top.row(align=True)
                    row.prop(scene, "UVTexIndex", text="UVTexCoord")

                    col_top = box.column(align=True)
                    row = col_top.row(align=True)
                    row.operator("uvutil.select_name", text="Select UV Name")
                    row = col_top.row(align=True)
                    row.prop(scene, "UVTexGetName", text="")

                    col_top = box.column(align=True)
                    row = col_top.row(align=True)
                    row.operator("uvutil.remove_active", text="Remove Active UV")

######  SureUVW  ######-------------------------------------------------
######  SureUVW  ######-------------------------------------------------

                if lt.display_tab_uvsure:
                    box = layout.box()
                    row = box.row()
                    row.prop(lt, "display_tab_uvsure", text="", icon='DISCLOSURE_TRI_DOWN_VEC')
                else:
                    box = layout.box()
                    row = box.row()
                    row.prop(lt, "display_tab_uvsure", text="", icon='DISCLOSURE_TRI_RIGHT_VEC')

                row.label("SureUVW...")

                if lt.display_tab_uvsure:

                    col = layout.column(align=True)
                    box = col.column(align=True).box().column()
                    col_top = box.column(align=True)

                    row = col_top.row(align=True)

                    row.label("Press this button first:")
                    row = col_top.row(align=True)
                    row.operator("object.sureuvw_operator",text="Show active texture on object").action='showtex'
                    row = col_top.row(align=True)
                    row.label("UVW Mapping:")
                    row = col_top.row(align=True)
                    row.operator("object.sureuvw_operator",text="UVW Box Map").action='box'
                    row = col_top.row(align=True)
                    row.operator("object.sureuvw_operator",text="Best Planar Map").action='bestplanar'
                    row = col_top.row(align=True)
                    row.label("1. Make Material With Raster Texture!")
                    row = col_top.row(align=True)
                    row.label("2. Set Texture Mapping Coords: UV!")
                    row = col_top.row(align=True)
                    row.label("3. Use Addon buttons")

#################################-------------------------------------------------------
#######  UV Mapping Edit  #######-------------------------------------------------------
#######  UV Mapping Edit  #######-------------------------------------------------------
#################################-------------------------------------------------------

### Editmode ###

        obj = context
        if obj and obj.mode == 'EDIT_MESH':

            if lt.display_tab_unwrap:
                box = layout.box()
                row = box.row()
                row.prop(lt, "display_tab_unwrap", text="", icon='TRIA_DOWN')
            else:
                box = layout.box()
                row = box.row()
                row.prop(lt, "display_tab_unwrap", text="", icon='TRIA_RIGHT')

            row.label("UVTools...")

            if lt.display_tab_unwrap:

                col = layout.column(align=True)
                box = col.column(align=True).box().column()
                col_top = box.column(align=True)

                row = col_top.row(align=True)

                row.operator("mesh.mark_seam").clear = False
                row.operator("mesh.mark_seam", text="Clear Seam").clear = True

                box = col.column(align=True).box().column()
                col_top = box.column(align=True)

                row = col_top.row(align=True)
                row.operator("uv.copy_uv")
                row.operator("uv.paste_uv")

                row = col_top.row(align=True)
                row.operator("uv.copy_uv_sel_seq","Copy UV Sel. Seq.")
                row.operator("uv.paste_uv_sel_seq","Paste UV Sel. Seq.")

                row = col_top.row(align=True)
                row.menu("uv.copy_uv_map","Copy UV Map")
                row.menu("uv.paste_uv_map","Paste UV Map")

                box = col.column(align=True).box().column()
                col_top = box.column(align=True)

                row = col_top.row(align=True)
                row.alignment = 'CENTER'
                row.operator("uv.reproject_image", text ="Reproject Image")

######  Unwrap  ######-------------------------------------------------
######  Unwrap  ######-------------------------------------------------

                if lt.display_tab_unwrapset:
                    box = layout.box()
                    row = box.row()
                    row.prop(lt, "display_tab_unwrapset", text="", icon='DISCLOSURE_TRI_DOWN_VEC')
                else:
                    box = layout.box()
                    row = box.row()
                    row.prop(lt, "display_tab_unwrapset", text="", icon='DISCLOSURE_TRI_RIGHT_VEC')

                row.label("Unwrap...")

                if lt.display_tab_unwrapset:

                    col = layout.column(align=True)
                    box = col.column(align=True).box().column()
                    col_top = box.column(align=True)

                    row = col_top.row(align=True)
                    row.operator("uv.unwrap", text="Unwrap")
                    row.operator("uv.reset",text="Reset")

                    row = col_top.row(align=True)
                    row.operator("uv.smart_project", text="Smart UV Project")

                    row = col_top.row(align=True)
                    row.operator("uv.lightmap_pack", text="Lightmap Pack")

                    row = col_top.row(align=True)
                    row.operator("uv.follow_active_quads", text="Follow Active Quads")

                    box = col.column(align=True).box().column()
                    col_top = box.column(align=True)

                    row = col_top.row(align=True)
                    row.operator("uv.cube_project", text="Cube Project")

                    row = col_top.row(align=True)
                    row.operator("uv.cylinder_project", text="Cylinder Project")

                    row = col_top.row(align=True)
                    row.operator("uv.sphere_project", text="Sphere Project")

                    row = col_top.row(align=True)
                    row.operator("uv.tube_uv_unwrap", text="Tube Project")

                    box = col.column(align=True).box().column()
                    col_top = box.column(align=True)

                    row = col_top.row(align=True)
                    row.operator("uv.project_from_view", text="Project from View").scale_to_bounds = False

                    row = col_top.row(align=True)
                    row.operator("uv.project_from_view", text="Project from View > Bounds").scale_to_bounds = True

######  TexSpace / Freestyle  ######-------------------------------------------------
######  TexSpace / Freestyle  ######-------------------------------------------------

                if lt.display_tab_uvnext:
                    box = layout.box()
                    row = box.row()
                    row.prop(lt, "display_tab_uvnext", text="", icon='DISCLOSURE_TRI_DOWN_VEC')
                else:
                    box = layout.box()
                    row = box.row()
                    row.prop(lt, "display_tab_uvnext", text="", icon='DISCLOSURE_TRI_RIGHT_VEC')

                row.label("TexSpace... / Freestyle")

                if lt.display_tab_uvnext:

                    col = layout.column(align=True)
                    box = col.column(align=True).box().column()
                    col_top = box.column(align=True)

                    row = col_top.row(align=True)
                    row.label(text="Texture Space:")

                    row = col_top.row(align=True)
                    row.operator("transform.translate", text ="Move").texture_space = True
                    row.operator("mesh.mark_seam", text="Scale").clear = True

                    col_top = box.column(align=True)
                    row = col_top.row(align=True)
                    row.label(text="Freestyle:")

                    row = col_top.row(align=True)
                    row.operator("mesh.mark_freestyle_face", text="Mark Face").clear=False
                    row.operator("mesh.mark_freestyle_face", text="Clear Face").clear=True

############------------############
############  REGISTER  ############
############------------############

def register():
    bpy.utils.register_class(META_TAB_UVS)
    bpy.utils.register_class(DropdownMetaTool_UV_Props)
    bpy.types.WindowManager.metawindowtool_uv = bpy.props.PointerProperty(type = DropdownMetaTool_UV_Props)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()


import os, sys
from fabric.api import env as fabenv
from fabric.api import local
from fabric.context_managers import settings

sys.path.append("../..")

from library.utils.fabric.host_constants import Hosts

fabenv.roledefs = Hosts.apps

# print "Roles:", fabenv.roles[0]
# print "Path:", sys.path
# print "Hosts: ", Hosts.apps[fabenv.roles[0]]
#print "Roledefs: ", fabenv.roledefs

#params
hostname="localhost"
app_name="rabbitmq"
image_name="rabbitmq:3-management"

docker_params=" --hostname rabbitmq --name rabbitmq -p 15672:15762 rabbitmq:3-management "

def deploy():
    if "local" in fabenv.roles:
        deploy_local()

def deploy_local():
    with settings(warn_only=True):
        local("docker rm --force {app_name}".format(
            app_name=app_name
        ))

        local("docker run -d {docker_params}".format(
            docker_params=docker_params
        ))


from oscar.apps.shipping import repository

from tga_custom_apps.shipping import methods

class Repository(repository.Repository):

    def get_available_shipping_methods(
            self, basket, user=None, shipping_addr=None,
            request=None, **kwargs):
        methods_f = [methods.Standard(), methods.Express()]
        return methods_f


#GIMP - usar p fazer fundo transparente
import pygame
import os

pygame.init()
vec = pygame.math.Vector2

largura = 800
altura = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

screen = pygame.display.set_mode((956, 560))

pygame.display.set_caption('Jogo da Bruxa')
clock = pygame.time.Clock()

game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, "img")

class Player(pygame.sprite.Sprite):
	def __init__ (self):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load(os.path.join(img_folder,"PrimeiraBruxa.png")).convert()
		self.image.set_colorkey(black)
		self.rect = self.image.get_rect()
		self.rect.center = (largura/2, altura/2)
		self.y_speed = 5

	def update(self):
		self.rect.y += self.y_speed
		if self.rect.bottom > altura - 40:
			self.y_speed = 0
		if self.rect.top < 50:
			self.y_speed = 5
		#vec = pygame.math.Vector2
		#self.pos = vec(largura/2, altura/2)
		#self.rect.midbottom = self.pos

class Platform(pygame.sprite.Sprite):
	def __init__ (self, x, y, widht, height):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load(os.path.join("plataforma.png")).convert()
		self.image.set_colorkey(white)
		self.rect = self.image.get_rect()
		self.rect.x = x
		self.rect.y = y

class Fundo():
	def __init__(self):
		self.pos = vec(largura/2, altura/2)
		self.vel = vec(0,0)
		self.acc = vec(0,0)
	def update(self):
		self.acc = vec(0,0)
		self.vel += self.acc
		self.pos += self.vel + 0.5 * self.acc
		#self.

def background(x,y):
	screen.blit(backgroundload,(x,y))

def tijolo(xplat,yplat):
	screen.blit(tijoloload,(xplat,yplat))

backgroundload = pygame.image.load('bg_big.png')
tijoloload = pygame.image.load('plataforma.png')

all_sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
player = Player()

all_sprites.add(player)

def game_loop():
	x = (largura * 0.005)
	y = (altura * 0.0025)

	xplat = 250
	yplat = altura - 90

	x_change = 0
	y_change = 0

	gameExit = False
	while not gameExit:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				gameExit = True
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_LEFT:
					x_change = 5
				if event.key == pygame.K_RIGHT:
					x_change = -5
				if event.key == pygame.K_UP:
					y_change = 5
				if event.key == pygame.K_DOWN:
					y_change = -5
			if event.type == pygame.KEYUP:
				if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
					x_change = 0
				if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
					y_change = 0

		x += x_change
		y += y_change

		xplat += x_change
		yplat += y_change
		p1 = Platform(xplat,yplat,300, 30)

		pygame.display.update()
		all_sprites.update()

		hits = pygame.sprite.spritecollide(player,platforms,False)
		if hits:
			#player.pos.y = hits[0].rect.top
			player.y_speed = 0

		background(x,y)
		all_sprites.draw(screen)
		tijolo(xplat,yplat)
		all_sprites.draw(screen)
		clock.tick(60)

game_loop()
pygame.quit()
quit()

'''
Para mexer o oponente:
	self.rect.x += self.x_speed
		if self.rect.right > largura - 100:
			self.x_speed = -5
		if self.rect.left < 100:
			self.x_speed = 5
'''


import socket
import struct
serverName = '255.255.255.255'
serverPort = 67
clientport = 68
addr = (serverName,clientport)
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serverSocket.bind(('',serverPort))

Mac_list = []
num = 1
#get serverIP
ip = socket.gethostbyname(socket.gethostname())
packedIP = socket.inet_aton(ip) #32-bit packed binary format
IPdata = packedIP[0:4]
#Mac_list.append(ord(packedIP[3]))  #ord():unicode

while 1:
     Sdata = serverSocket.recv(2048)

     #receive Discover message
     print("receive DISCOVER message")
     TransID = Sdata[4:8]
     NextServerIP = socket.inet_aton(socket.gethostbyname(socket.gethostname()))
     MACaddr = Sdata[28:34]

     #DHCP_OFFER message
     print("send Offer message")
     OP = b'\x02'
     HTYPE = b'\x01'
     HLEN = b'\x06'
     HOPS = b'\x00'
     TRANSACTION_ID = TransID

     SECS = b'\x00\x00'
     FLAGS = b'\x00\x00'
     CIADDR = b'\x00\x00\x00\x00'

     if 150 + num in Mac_list:
          num = num + 1

     YIADDR = IPdata[0:3] + struct.pack('B',150 + num) # str
     Mac_list.append(150+num)
     Assign_IP = YIADDR
     num = num + 1

     SIADDR = NextServerIP
     GIADDR = b'\x00\x00\x00\x00'

     CHADDR = MACaddr
     CHADDR += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

     SNAME = b'\x00' * 64
     FILE = b'\x00' * 128
     Magic_Cookie = b'\x63\x82\x53\x63'
     Option = b'\x35\x01\x02' #53-Offer
     Option += b'\x36\x04' + packedIP #54 DHCPServerIdentifier
     Option += b'\x33\x04\x00\x00\x1a\x23' #51 Lease time
     Option += b'\x03\x04' + packedIP #3 router
     Option += b'\x01\x04\xff\xff\xff\x00' #1 subnet mask
     Option += b'\x06\x0c'  + socket.inet_aton('9.7.10.15') #6 DNS servername
     Option += socket.inet_aton('9.7.10.16')
     Option += socket.inet_aton('9.7.10.18')
     Option += b'\xff' #end

     sendOfferList = OP+HTYPE+HLEN+HOPS+TRANSACTION_ID+SECS+FLAGS+CIADDR+YIADDR+SIADDR+GIADDR+CHADDR+SNAME+FILE+Magic_Cookie+Option
     serverSocket.sendto(sendOfferList,addr)

     while 1:
          #receive REQUEST message
          print("receive REQUEST message")
          AData = serverSocket.recv(2048)
          TransID = AData[4:8]
          NextServerIP = AData[20:24]
          MACaddr = AData[28:34]
          DHCPServerIdentifier = AData[245:249]

          #DHCP_ACK message
          print("send ACK message")

          OP = b'\x02'
          HTYPE = b'\x01'
          HLEN = b'\x06'
          HOPS = b'\x00'
          TRANSACTION_ID = TransID

          SECS = b'\x00\x00'
          FLAGS = b'\x00\x00'
          CIADDR = b'\x00\x00\x00\x00'

          YIADDR = Assign_IP
          SIADDR = NextServerIP
          GIADDR = b'\x00\x00\x00\x00'

          CHADDR = MACaddr
          CHADDR += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

          SNAME = b'\x00' * 64
          FILE = b'\x00' * 128
          Magic_Cookie = b'\x63\x82\x53\x63'
          Option = b'\x35\x01\x05' #53-ACK
          Option += b'\x36\x04' + DHCPServerIdentifier #54
          Option += b'\x33\x04\x00\x00\x1a\x23' #51
          Option += b'\x03\x04' + packedIP #3
          Option += b'\x01\x04\xff\xff\xff\x00' #1
          Option += b'\x06\x0c' + socket.inet_aton('9.7.10.15')#6
          Option += socket.inet_aton('9.7.10.16')
          Option += socket.inet_aton('9.7.10.18')
          Option += b'\xff' #end

          sendACKList = OP+HTYPE+HLEN+HOPS+TRANSACTION_ID+SECS+FLAGS+CIADDR+YIADDR+SIADDR+GIADDR+CHADDR+SNAME+FILE+Magic_Cookie+Option
          serverSocket.sendto(sendACKList,addr)
          break

serverSocket.close()


def knight(board, r, c, moves, nr):
        dim = len(board)
        if r < 0 or r >= dim or c < 0 or c >= dim or board[r][c]:
            return
        board[r][c] = True
        moves[r][c] = nr
        if nr == dim*dim:
            for i in range(dim):
                for j in range(dim):
                    print("%3d" % moves[i][j], end=" ")
                print()
            print()
            #trzeba chyba false pole
            board[r][c] = False
            return

        jumps = ((-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1))
        for i in jumps:
            knight(board, r+i[0], c+i[1], moves, nr+1)

        board[r][c] = False

board = [[False for _ in range(5)] for _ in range(5)]
moves = [[0 for _ in range(5)] for _ in range(5)]
knight(board, 0, 0, moves, 1)


#code for different types of validation with small data sets
#hold-out validation, k-fold validation, iterated k-fold validation

#HOLD OUT VALIDATION
num_validation_samples = 10000
#shuffle the data
np.random.shuffle(data)
#define validation set
validation_data = data[:num_validation_samples]
data = data[num_validation_samples:]
#define training set
training_data = data[:]
#train on training data
model = get_model()
model.train(training_data)
#evaluate on valdation data
validation_score = model.evaluate(validation_data)
#re-train from scratch after after tuning hyperparameters
model = get_model()
model.train(np.concatenate([training_data, validation_data]))
test_score = model.evaluate(test_data)

#K-FOLD CROSS-VALIDATION
k = 4
num_validation_samples = len(data) // k
np.random.shuffle(data)
validaton_scores = []
#select validation data partition
for fold in range(k):
	validation_data = data[num_validation_samples * fold: num_validation_samples * (fold + 1)]
#use remaining data as training data
	training_data = data[:num_validation_samples * fold] + data[num_validation_samples * (fold + 1):]
#create untrained model
	model = get_model()
	model.train(training_data)
	validation_score = model.evaluate(validation_data)
	validaton_scores.append(validation_score)
#validation score average of k folds
	validation_score = np.average(validation_scores)
	model = get_model()
	model.train(data)
	test_score = model.evaluate(test_data)

#iterated k-fold applies k-fold multiple times

ch 4 different types of validation sets p98.py


import feedparser
import json
import datetime
import time

def rss2json(url):

    """
    rss atom to parsed json data
    supports google alerts
    """
    parsedurl = feedparser.parse(url)
    item = {}
    feedslist = []
    feed = {}
    feedsdict = {}

    feed["status"] = "ok"
    if 'updated' in parsedurl.feed.keys():
        feed["date"] = parsedurl.feed.updated
    if 'title' in  parsedurl.feed.keys():
        feed["title"]=parsedurl.feed.title
    if 'image' in parsedurl.feed.keys():
        feed["image"] =parsedurl.feed.image

#     feeds.append(feed.copy())
    feedsdict["data"] = feed
    for fd in parsedurl.entries:
        item["title"]=fd.title
        item["link"] = fd.link
        item["summary"]=fd.summary
        item["published"]=time.mktime(datetime.datetime.strptime(fd.published, "%a, %d %b %Y %H:%M:%S %z").timetuple())

        if 'links' in fd and 'image' in fd.links:
            item["thumbnail"]=feed.links[1]
        if 'storyimage' in fd.keys():
            item["thumbnail"] = fd["storyimage"]

        feedslist.append(item.copy())

    feedsdict["feeds"] = feedslist

    return json.dumps(feedsdict)


def cal(ai, bi, d):
    if r - ai[bi.index(min(bi))] >= d:
        return d * min(bi)
    else:
        minb = min(bi)
        d -= r - ai[bi.index(minb)]
        count = (r - ai[bi.index(minb)]) * minb
        ai.pop(bi.index(minb))
        bi.pop(bi.index(minb))
        return count + cal(ai, bi, d)
    return

str1 = input().split()
n = int(str1[0])
r = int(str1[1])
avg = int(str1[2])
ai = []
bi = []
nowsum = 0
for x in range(n):
    str2 = input().split()
    ai.append(int(str2[0]))
    nowsum += int(str2[0])
    bi.append(int(str2[1]))
needsum = avg * n
d = needsum - nowsum
if d <= 0:
    print(0)
else:
    print(cal(ai, bi, d))


"""
Test the somo module.
"""

import numpy as np
import random as r
from sklearn.datasets import make_classification
from ...over_sampling import SOMO

def test_random_state():
    """Test random state initialization."""
    rand_state = r.randint(0,1000)
    somo = SOMO(random_state=rand_state)
    assert somo.random_state == rand_state

def test_output():
    """Test correct output shape of generated data."""
    len_X = r.randint(3,10)
    X, y = make_classification(n_classes=3, class_sep=2,
                               weights=[0.7, 0.3], n_informative=len_X, n_redundant=0, flip_y=0.1,
                               n_features=len_X, n_clusters_per_class=1, n_samples=200, random_state=10)
    somo = SOMO(som_rows=20, som_cols=20)
    X_res, y_res = somo.fit_sample(X, y)
    assert np.shape(X_res)[1] == len_X

def test_output_ratio():
    """Test Correct ratio of generated synthetic data."""
    X, y = make_classification(n_classes=3, class_sep=2,
                               weights=[0.6, 0.2, 0.2], n_informative=4, n_redundant=0, flip_y=0.1,
                               n_features=5, n_clusters_per_class=1, n_samples=200, random_state=None)
    somo = SOMO(ratio = 'auto', som_rows=20, som_cols=20)
    X_res, y_res = somo.fit_sample(X, y)
    count = [len(y_res[y_res == num]) for num in set(y_res)]
    ratio = [(num/max(count)) for num in count]
    assert 0.99 < np.average(ratio) <= 1

def test_handle_negative_values():
    """Test handle negative values."""
    X, y = make_classification(n_classes=3, class_sep=2,
                               weights=(0.6,0.2,0.2), n_informative=4, n_redundant=0, flip_y=0.1,
                               n_features=5, n_clusters_per_class=1, n_samples=200, random_state=None, shift = -10)
    somo = SOMO(ratio='auto', som_rows=20, som_cols=20)
    X_res, y_res = somo.fit_sample(X, y)
    assert X.mean() < 0
    assert X_res.mean() < 0

def test_multiple_classes():
    """Test validate multiple target classes."""
    for num in range(2,10):
        X, y = make_classification(n_classes=num, class_sep=2, n_informative=4, n_redundant=0, flip_y=0.1,
                                   n_features=5, n_clusters_per_class=1, n_samples=200, random_state=None)
        somo = SOMO(ratio='auto', som_rows=20, som_cols=20)
        X_res, y_res = somo.fit_sample(X, y)
        assert set(y) == set(y_res)


from flask import Flask, request, jsonify
from flask_marshmallow import Marshmallow
import requests

order = Flask(__name__)

Order_IP = '192.168.1.115'          #second order server(host)
Cat_IP = '192.168.1.114:5000'       #second catalog server

@order.route('/buy/<id>' ,methods=['POST','GET'])   #received buy request
def get_quantity(id):
    request = 'http://' + Cat_IP + '/quantity/' + id        #generating the quantity request
    quantity = int(requests.get(request).content)           #sending a request to catalog to server to know the quantity of the book
    if quantity > 0:                                        #chacks if the ammount if sufficient
        request = 'http://' + Cat_IP + '/update/' + id      #generating the update request
        requests.get(request)                               #sending the request the catalog server
        return 'The book has been purchased successfully'   #return response to front-end server
    else:
        return 'This book is out of stock'                  #return response to front-end server

if __name__ == "__main__":
    order.run(host = Order_IP)
    order.run(debug=True)


import hypothesis
from hypothesis import given, assume, strategies
from hypothesis.strategies import integers
import pytest

from saleae.register_decoder import RegisterMap, Register

@strategies.composite
def register_maps(
    draw,
    register_gap=integers(min_value=0, max_value=100),
    register_width=integers(min_value=1, max_value=100),
    register_count=integers(min_value=0, max_value=10),
):
    current_addr = 0
    # Generate some registers prior to the targeted register
    registers = []
    for _ in range(draw(integers(min_value=0, max_value=10))):
        # Skip some address entries
        current_addr += draw(register_gap)
        # Add a register
        address_width = draw(register_width)
        registers.append(Register(current_addr, address_width=address_width))
        current_addr += address_width

    members = {}
    for register in registers:
        members[draw(strategies.from_regex(r"[A-Za-z_]+"))] = register

    return RegisterMap.__class__("MyRegMap", (RegisterMap,), members)


#  Hint:  You may not need all of these.  Remove the unused functions.
from hashtables import (HashTable,
                        hash_table_insert,
                        hash_table_remove,
                        hash_table_retrieve,
                        hash_table_resize)

class Ticket:
    def __init__(self, source, destination):
        self.source = source
        self.destination = destination

def reconstruct_trip(tickets, length):
    ht = HashTable(length)
    route = [None] * length

    current_ticket = None

    # for every ticket in tickets
    # add it to the hash table key = source, value = destination
    for i in range(length):
        hash_table_insert(ht, tickets[i].source, tickets[i].destination)
        # when you find ticket with current location (NONE)
        if tickets[i].source == "NONE":
            # add it to variable `current_ticket`
            current_ticket = tickets[i].destination
            # and make it the first destination of the route
            route[0] = current_ticket

    # then loop over the tickets again
    for i in range(1, length):
        # find the next destination
        dest = hash_table_retrieve(ht, current_ticket)
        # add it to the route array
        route[i] = dest
        # and make it current_ticket
        current_ticket = dest

    return route


from flask import Flask
# we import the Twilio client from the dependency we just installed
from flask import request
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
app = Flask(__name__)
@app.route('/',methods=['GET'])
def hello_world():

     return 'Hello, World!'
@app.route('/sendmail', methods=['POST'])
def send_mail():
    # Python code to illustratdde Sending mail from
    # your Gmail account
    print(request.json)
    data=request.json
    name=data['name']
    phone_no=data['phone_no']
    email = data['email']
    message = MIMEMultipart()
    message['From'] = email
    message['To'] = "justolearnpune@gmail.com"
    message['Subject'] = 'Inquery Justolearn.'  # The subject line
    # The body and the attachments for the mail
    mail_content = "Recived Enquire from  " + " name: " + name + " email: " + email + "phone_no" + phone_no
    message.attach(MIMEText(mail_content, 'plain'))
    import smtplib

    # creates SMTP session
    s = smtplib.SMTP('smtp.gmail.com', 587)
    s.ehlo()
    # start TLS for security
    s.starttls()
    s.ehlo()
    # Authentication
    s.login("justolearnpune@gmail.com", "justo.007")

    # message to be sent
    text = message.as_string()
    # sending the mail
    s.sendmail(email, "justolearnpune@gmail.com", text)

    # terminating the session
    s.quit()
    return  "mail sent"

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


#1. 특정한 지점의 주변 상,하,좌,우를 살펴본 뒤에 주변 지점 중에서 값이 '0'이면서 아직 방문하지 않은 지점이 있다면 해당 지점을 방문한다.
#2. 방문한 지점에서 다시 상,하,좌,우를 살펴보면서 방문을 다시 진행하면, 연결된 모든 지점을 방문할 수 있다.
#3. 1~2번의 과정을 모든 노드에 반복하며 방문하지 않은 지점의 수를 센다.

#입력값 선언
n,m = map(int,input().split())
graph = []

#2차원 리스트의 맵 정보 입력받기.
for i in range(n):
  graph.append(list(map(int,input())))

#DFS로 특정한 노드를 방문한 뒤에 연결된 모든 노드들도 방문
def dfs(x,y):
  #주어진 범위를 벗어나는 경우에는 즉시 종료
  if x <= -1 or x >= n or y <= -1 or y >= m:
    return False
  #현재 노드를 아직 방문하지 않았다면
  if graph[x][y] == 0:
    #해당 노드 방문 처리
    graph[x][y] = 1
    #상,하,좌,우의 위치도 모두 재귀적으로 호출
    dfs(x-1,y)
    dfs(x+1,y)
    dfs(x,y-1)
    dfs(x,y+1)
    return True
  return False

  #모든 노드에 대하여 음료수 채우기
result = 0
for i in range(n):
  for j in range(m):
    #현재 위치에서 dfs 실행
    if dfs(i,j)==True:
      result +=1
print(result)


import os
import sys
import pytest
import pickle
import numpy as np
import datetime as dt
from attrdict import AttrDict
from .pytest_utils import fp, assert_objects_equal, call_with_legacy_params
from SEIRcity import model, utils

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

def test_model_can_import():
    """Can import the two functions in SEIR_main_publish
    """
    assert hasattr(model, "SEIR_model_publish_w_risk")
    assert hasattr(model, "compute_R0")

@pytest.mark.skip
@pytest.mark.parametrize("legacy_pickle", [
    # Call from SEIR_main_publish.main on commit on commit eca0e3b4222476b87d45
    fp("tests/data/SEIR_model_publish_w_risk_result0.pckl")
])
def test_model_publish_w_risk_legacy(legacy_pickle, tmp_path):
    """Compare results of legacy run of model.SEIR_model_publish_w_risk
    with results from a new run. Args, kwargs, and results were written
    to file path `legacy_pickle` by dev_utils.result_to_pickle. Can the
    same function yield exactly the same outputs as the legacy run using
    exactly the same inputs. Equality is determined by
    pytest_utils.assert_objects_equal.
    """
    assert os.path.isfile(legacy_pickle)
    legacy_result, new_result = call_with_legacy_params(
        legacy_pickle=legacy_pickle, func=model.SEIR_model_publish_w_risk)
    assert isinstance(legacy_result, tuple)
    assert isinstance(new_result, tuple)
    assert_objects_equal(legacy_result, new_result, verbose=False)

def test_dt_to_dt64arr_and_back():
    """Can convert from dt64 to dt and back.
    """
    # we use the legacy_reopen datetime from below test as fixture
    as_dt = dt.datetime(2020, 4, 2)
    print('as_dt: ', as_dt)
    as_dt64 = utils.dt_to_dt64(as_dt)
    print('as_dt64: ', as_dt64)
    back_again = utils.dt64_to_dt(as_dt64)
    assert back_again == as_dt

@pytest.mark.skip
@pytest.mark.parametrize("legacy_pickle", [
    # Call from SEIR_main_publish.main on commit on commit eca0e3b4222476b87d45
    fp("tests/data/SEIR_model_publish_w_risk_result0.pckl")
])
def test_model_can_return_dt64_array(legacy_pickle, tmp_path):
    """If the format of school event times returned by model is changed
    to an ndarray with dtype datetime64, can we recapitulate the old
    datetime.datetime return format?
    """
    # run model with same kwargs
    assert os.path.isfile(legacy_pickle)
    legacy_result, new_result = call_with_legacy_params(
        legacy_pickle=legacy_pickle,
        func=model.SEIR_model_publish_w_risk)

    legacy_reopen = legacy_result[-1]
    legacy_close = legacy_result[-2]
    print('legacy_reopen: ', legacy_reopen)
    print('legacy_close: ', legacy_close)

    new_reopen_arr = new_result[-1]
    new_close_arr = new_result[-2]
    assert isinstance(new_reopen_arr, np.ndarray)
    assert isinstance(new_close_arr, np.ndarray)
    assert new_reopen_arr.dtype == 'float64'
    assert new_close_arr.dtype == 'float64'

    new_reopen_dt = utils.bool_arr_to_dt(new_reopen_arr)
    new_close_dt = utils.bool_arr_to_dt(new_close_arr)

    assert new_reopen_dt == legacy_reopen
    assert new_close_dt == legacy_close


import socket

target_host = "localhost"
target_port = 9998

# Create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the client
client.connect((target_host,target_port))

# Send some data
client.send(b"Sup?")#b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")

# Recieve data
response = client.recv(4096)

print(response.decode())
client.close()


versions =\
{
  'python' : ['2.7.5', '3.3.2'],
  'ruby'   : ['1.9.3-p194', '1.9.3-p392'],
}

extensions =\
{
  'python' : 'py',
  'ruby'   : 'rb',
}

manager =\
{
  'python' : 'source venv/bin/activate',
  'ruby'   : 'source /etc/profile.d/rbenv.sh && rbenv rehash'
}

ports =\
{
  'postgresql' : '5432',
  'redis'      : '6379',
  'mysql'      : '3306',
}

HOST_DATABASE_FOLDER = '/opt/crane/databases'

volumes =\
{
  'postgresql' : '/var/lib/postgresql'
}


import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.datasets import *
from sklearn.neighbors import KNeighborsRegressor
import numpy as np
import csv

df = pd.read_csv("praguetrain.tsv", sep='\t')
df.columns = ["area","construction","ownership","status","floor","equipment","cellar","balcony","son","nan"]

labelEncoder = LabelEncoder()
labelEncoder.fit(df['construction'])
df['construction'] = labelEncoder.transform(df['construction'])
labelEncoder.fit(df['ownership'])
df['ownership'] = labelEncoder.transform(df['ownership'])
labelEncoder.fit(df['status'])
df['status'] = labelEncoder.transform(df['status'])
labelEncoder.fit(df['equipment'])
df['equipment'] = labelEncoder.transform(df['equipment'])
labelEncoder.fit(df['cellar'])
df['cellar'] = labelEncoder.transform(df['cellar'])
labelEncoder.fit(df['balcony'])
df['balcony'] = labelEncoder.transform(df['balcony'])

df = df.iloc[1:1000,:]

train_x = df.drop(["nan","son"],1)
train_y = df.drop(["area","construction","ownership","status","floor","equipment","cellar","balcony","nan"],1)

KNR = KNeighborsRegressor(30)
KNR.fit(train_x,train_y)
KNeighborsRegressor(algorithm='auto',leaf_size =30, metric='minkowski',metric_params=None, n_jobs=1,n_neighbors=20, p =2,weights='uniform')

heyyo = pd.read_csv("praguetest.tsv", sep='\t')

heyyo.columns = ["area","construction","ownership","status","floor","equipment","cellar","balcony","son","nan"]

labelEncoder.fit(heyyo['construction'])
heyyo['construction'] = labelEncoder.transform(heyyo['construction'])
labelEncoder.fit(heyyo['ownership'])
heyyo['ownership'] = labelEncoder.transform(heyyo['ownership'])
labelEncoder.fit(heyyo['status'])
heyyo['status'] = labelEncoder.transform(heyyo['status'])
labelEncoder.fit(heyyo['equipment'])
heyyo['equipment'] = labelEncoder.transform(heyyo['equipment'])
labelEncoder.fit(heyyo['cellar'])
heyyo['cellar'] = labelEncoder.transform(heyyo['cellar'])
labelEncoder.fit(heyyo['balcony'])
heyyo['balcony'] = labelEncoder.transform(heyyo['balcony'])

heyyo = heyyo.drop(["nan"],1)

"////////////////////////////////////////////////////////////////////////////////"

for i in range(40):
    test=np.array([heyyo.iloc[i][0],heyyo.iloc[i][1],heyyo.iloc[i][2],heyyo.iloc[i][3],heyyo.iloc[i][4],heyyo.iloc[i][5],heyyo.iloc[i][6],heyyo.iloc[i][7]])
    target = heyyo.iloc[i][8]
    test1 = test.reshape(1,-1)
    hm = int(KNR.predict(test1))
    print("Predicted:",hm,"| Actual:",target)


from camera import kinect
import sys
ros_cv2_path = '/opt/ros/kinetic/lib/python2.7/dist-packages'
if ros_cv2_path in sys.path:
    sys.path.remove(ros_cv2_path)
    import cv2
    sys.path.append(ros_cv2_path)
else:
    import cv2
import numpy as np
import os
from PIL import Image
import time

camera = kinect.Kinect(1026,"../config/intrinsic_real_world.yml")
save_dir = "../real_data"
timestamp = time.time()
timestruct = time.localtime(timestamp)
time_str = time.strftime('%m_%d_%H_%M', timestruct)
save_dir = os.path.join(save_dir,time_str)
os.makedirs(save_dir)
while(True):
    flag, rgb,depth = camera.get_rgb_depth_image()
    if not flag:
        continue
    img_resize = cv2.resize(rgb,(512,512))
    cv2.imshow("rgb",img_resize)
    key = cv2.waitKey(100)
    print(key)
    if key == 115:
        timestamp = time.time()
        timestruct = time.localtime(timestamp)
        time_str = time.strftime('%m_%d_%H_%M_%S', timestruct)
        cv2.imwrite(os.path.join(save_dir,"{}_color.bmp".format(time_str)),rgb)
        depth_image = Image.fromarray(depth)
        depth_image.save(os.path.join(save_dir,"{}_depth.png".format(time_str)))
    if key == 112:
        break
camera.realease()


import tweepy
import pandas
import json # The API returns JSON formatted text
import os

cwd = os.getcwd()
os.chdir('kafka')
os.system('bin\windows\kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 4 --topic tweets')
os.chdir(cwd)

access_token = "Get your own"
access_token_secret =  "Get your own"
consumer_key =  "Get your own"
consumer_secret =  "Get your own"

# Pass OAuth details to tweepy's OAuth handler
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

TRACKING_KEYWORDS = ['covid']
OUTPUT_FILE = "tweets.txt"
TWEETS_TO_CAPTURE = 5000

class MyStreamListener(tweepy.StreamListener):
    """
    Twitter listener, collects streaming tweets and output to a file
    """
    def __init__(self, api=None):
        super(MyStreamListener, self).__init__()
        self.num_tweets = 0
        self.file = open(OUTPUT_FILE, "w")

    def on_status(self, status):
        tweet = status._json
        self.file.write( json.dumps(tweet) + '\n' )
        self.num_tweets += 1

        # Stops streaming when it reaches the limit
        if self.num_tweets <= TWEETS_TO_CAPTURE:
            if self.num_tweets % 100 == 0: # just to see some progress...
                print('Numer of tweets captured so far: {}'.format(self.num_tweets))
            return True
        else:
            return False
        self.file.close()

    def on_error(self, status):
        print(status)

#%%time #let's see how long it takes

# Initialize Stream listener
l = MyStreamListener()

# Create you Stream object with authentication
stream = tweepy.Stream(auth, l)

# Filter Twitter Streams to capture data by the keywords:
stream.filter(track=TRACKING_KEYWORDS)

import matplotlib.pyplot as plt
# Initialize empty list to store tweets
tweets_data = []
OUTPUT_FILE = "tweets.txt"
# Open connection to file
with open(OUTPUT_FILE, "r") as tweets_file:
    # Read in tweets and store in list
    for line in tweets_file:
        tweet = json.loads(line)
        tweets_data.append(tweet)

df = pandas.DataFrame(tweets_data, columns=['created_at','lang', 'text', 'source'])
# Just convert to datetime
df['created_at'] = pandas.to_datetime(df.created_at)
# Regular expression to get only what's between HTML tags: > <
df['source'] = df['source'].str.extract('>(.+?)<', expand=False).str.strip()
print(df.head())

html = df.to_html()

#write html to file
text_file = open("index.html", "w",encoding='utf-8')
text_file.write(html)
text_file.close()

# create filter for most popular languages
lang_mask = (df.lang == 'en') | (df.lang == 'ca') | (df.lang == 'fr') | (df.lang == 'es')

# create a filter for most popular sources
source_mask = (df.source == 'Twitter for iPhone') | (df.source == 'Twitter for Android')\
    | (df.source == 'Twitter Web Client') | (df.source == 'Twitter for iPad') \
    | (df.source == 'Twitter Lite') | (df.source == 'Tweet Old Post')

(df[lang_mask & source_mask].groupby(['source','lang']) # apply filter/groupby
 .size() # get count of tweets per source/lang
 .unstack() # unstack to create new DF
 .fillna(0) # fill NaN with 0
 .plot(kind='bar', figsize=(14,7), title='Tweets by source and language') # plot
)

#df.plot( kind = 'bar')
plt.savefig('pic.png')
plt.show()


import os
import csv

class Logger(object):
    ''' Logger saves the running results and helps make plots from the results
    '''

    def __init__(self, log_dir):
        ''' Initialize the labels, legend and paths of the plot and log file.

        Args:
            log_path (str): The path the log files
        '''

        nr = 0
        nr = (str)(nr)
        self.log_dir = log_dir
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)

        new_path = os.path.join(log_dir, (nr))
        if not os.path.exists(new_path):
            os.makedirs(new_path)
        else:

            while(os.path.exists(new_path)):
                nr = (int)(nr) + 1
                nr = (str)(nr)
                new_path = os.path.join(log_dir, (nr))
            os.makedirs(new_path)
        log_dir = new_path
        self.log_dir = log_dir
        self.nr = nr
        # performance and parameters (average steps)
        self.txt_path = os.path.join(log_dir, ('log_avg_' + nr + '.txt'))
        self.csv_path = os.path.join(log_dir, ('csv_avg_' + nr +'.csv'))
        self.fig_path = os.path.join(log_dir, ('fig_avg_' + nr + '.png'))

        self.par_path = os.path.join(log_dir, ('parameters_' + nr + '.txt'))

        #loss
        self.txt_loss_path = os.path.join(log_dir, ('loss_log_' + nr + '.txt'))
        self.csv_loss_path = os.path.join(log_dir, ('loss_perf_' + nr + '.csv'))
        self.fig_loss_path = os.path.join(log_dir, ('fig_loss_' + nr + '.png'))

        # performance in 'percent', e.g. out of 100 run he could solve 40, e.g. 0.4
        self.txt_percent_path = os.path.join(log_dir, ('log_percent_' + nr + '.txt'))
        self.csv_percent_path = os.path.join(log_dir, ('csv_percent_' + nr + '.csv'))
        self.fig_percent_path = os.path.join(log_dir, ('fig_percent_' + nr + '.png'))

        # compare different figures in same graph
        self.fig_all_path = os.path.join(log_dir, ('fig_all_' + nr + '.png'))

        # best steps graph
        self.txt_best_path = os.path.join(log_dir, ('log_best_' + nr + '.txt'))
        self.csv_best_path = os.path.join(log_dir, ('csv_best_' + nr + '.csv'))
        self.fig_best_path = os.path.join(log_dir, ('fig_best_' + nr + '.png'))

        # random decisions % graph
        self.txt_rnd_decisions_path = os.path.join(log_dir, ('log_dec_' + nr + '.txt'))
        self.csv_rnd_decisions_path = os.path.join(log_dir, ('csv_dec_' + nr + '.csv'))
        self.fig_rnd_decisions_path = os.path.join(log_dir, ('fig_dec_' + nr + '.png'))

        # average episode steps
        self.txt_file = open(self.txt_path, 'w')
        self.csv_file = open(self.csv_path, 'w')
        self.par_file = open(self.par_path, 'w')
        fieldnames = ['episode', 'steps']
        self.writer = csv.DictWriter(self.csv_file, fieldnames=fieldnames)
        self.writer.writeheader()

        # episode loss
        self.txt_loss_file = open(self.txt_loss_path, 'w')
        self.csv_loss_file = open(self.csv_loss_path, 'w')
        fieldnames_loss = ['episode', 'loss']
        self.writer_loss = csv.DictWriter(self.csv_loss_file, fieldnames = fieldnames_loss)
        self.writer_loss.writeheader()

        # episode percent / solving accuracy
        self.txt_percent_file = open(self.txt_percent_path, 'w')
        self.csv_percent_file = open(self.csv_percent_path, 'w')
        fieldnames_percent = ['episode', 'percent']
        self.writer_percent = csv.DictWriter(self.csv_percent_file, fieldnames = fieldnames_percent)
        self.writer_percent.writeheader()

        # best steps
        self.txt_best_file = open(self.txt_best_path, 'w')
        self.csv_best_file = open(self.csv_best_path, 'w')
        fieldnames_best = ['episode', 'best']
        self.writer_best = csv.DictWriter(self.csv_best_file, fieldnames = fieldnames_best)
        self.writer_best.writeheader()

        # % random decisions
        self.txt_rnd_decisions_file = open(self.txt_rnd_decisions_path, 'w')
        self.csv_rnd_decisions_file = open(self.csv_rnd_decisions_path, 'w')
        fieldnames_rnd_decisions = ['episode', 'random decisions in %']
        self.writer_rnd_decisions = csv.DictWriter(self.csv_rnd_decisions_file, fieldnames = fieldnames_rnd_decisions)
        self.writer_rnd_decisions.writeheader()

    def return_new_path_dir(self):
        return self.log_dir

    def save_model(self, agent_model, name):
        path = os.path.join(self.log_dir,(name))
        agent_model.save(path)

    def log_par(self, parameter_text):
        ''' Write the parameter_text to the parameter file.
        Args:
            parameter_text(string): parameter text to log
        '''

        self.par_file.write(parameter_text+'\n')
        self.par_file.flush()
        print(parameter_text)

    def log_parameters(self, parameter_dict):
        ''' Log parameters into txt file
        Args:
            parameter_dict: multiple different parameters in parameter_dict
            dict? list with two dimensions?
        '''
        print('')
        self.log_par('--------------------')
        for key in parameter_dict:
            self.log_par('   ' + key + '  |  ' + str(parameter_dict[key]))
        print('')

    def log(self, text):
        ''' Write the text to log file then print it.
        Args:
            text(string): text to log
        '''
        self.txt_file.write(text+'\n')
        self.txt_file.flush()
        #print(text)

    def log_performance(self, episode, steps, epsilon):
        ''' Log a point in the curve
        Args:
            timestep (int): the timestep of the current point
            steps (float): the steps of the current point
        '''
        self.writer.writerow({'episode': episode, 'steps': steps})
        print('')
        self.log('----------------------------------------')
        self.log('  episodes   |  ' + str(episode))
        self.log('  avg steps |  ' + str(steps))
        self.log('  epsilon    |  ' + str(epsilon))
        self.log('----------------------------------------')

    def log_be(self, text):
        ''' Write the text to best file and then print it.
        Args:
            text(string): text to log
        '''
        self.txt_best_file.write(text+'\n')
        self.txt_best_file.flush()
        #print(text)

    def log_best(self, episode, best):
        ''' Log a point in the curve
        Args:
            episode (int): the episode of the current point
            best (float): best new steps
        '''
        self.writer_best.writerow({'episode': episode, 'best': best})
        print('')
        self.log_be('----------------------------------------')
        self.log_be('  episode    |  ' + str(episode))
        self.log_be('  best    |  ' + str(best))
        self.log_be('----------------------------------------')

    def log_dec(self, text):
        ''' Write the text to best file and then print it.
        Args:
            text(string): text to log
        '''
        self.txt_rnd_decisions_file.write(text+'\n')
        self.txt_rnd_decisions_file.flush()

    def log_rnd_decisions(self, episode, rnd_decisions):
        ''' Log a point in the curve
        Args:
            episode (int): the episode of the current point
            best (float): rnd decisions made in this episode
            :param episode:
            :param rnd_decisions:
        '''
        self.writer_rnd_decisions.writerow({'episode': episode, 'random decisions in %': rnd_decisions})
        print('')
        self.log_dec('----------------------------------------')
        self.log_dec('  episode    |  ' + str(episode))
        self.log_dec('  rnd_decisions in %    |  ' + str(rnd_decisions))
        self.log_dec('----------------------------------------')

    def log_perc(self, text):
        ''' Write the text to percent file and then print it.
        Args:
            text(string): text to log
        '''
        self.txt_percent_file.write(text+'\n')
        self.txt_percent_file.flush()
        #print(text)

    def log_percent(self, episode, percent):
        ''' Log a point in the curve
        Args:
            episode (int): the episode of the current point
            percent (float): accuracy of solving a episode
        '''
        self.writer_percent.writerow({'episode': episode, 'percent': percent})
        print('')
        self.log_perc('----------------------------------------')
        self.log_perc('  episodes                |  ' + str(episode))
        self.log_perc('  solving_accuracy in %   |  ' + str(percent))
        self.log_perc('----------------------------------------')

    def log_lo(self, text):
        ''' Write the text to log file then print it.
        Args:
            text(string): text to log
        '''
        self.txt_loss_file.write(text+'\n')
        self.txt_loss_file.flush()
        #print(text)

    def log_loss(self, episode, loss):
        ''' Log a point in the curve
        Args:
            episode (int): the episode of the current point
            loss (float): the loss of the current point
        '''
        self.writer_loss.writerow({'episode': episode, 'loss': loss})
        print('')
        self.log_lo('----------------------------------------')
        self.log_lo('  episode    |  ' + str(episode))
        self.log_lo('  loss      |  ' + str(loss))
        self.log_lo('----------------------------------------')

    def plot(self, plot, algorithm):
        print('plot: ', plot)
        if(plot=='percent'):
            plot_percent(self.csv_percent_path, self.fig_percent_path, algorithm)
            print('plotted solving rate')
        if(plot=='avg'):
            plot_avg(self.csv_path, self.fig_path, algorithm)
            print('plotted avg')
        if(plot=='best'):
            plot_best(self.csv_best_path, self.fig_best_path, algorithm)
            print('plotted best')
        if(plot=='dec%'):
            plot_dec(self.csv_rnd_decisions_path, self.fig_rnd_decisions_path, algorithm)
            print('plotted dec%')

    def close_files(self):
        ''' Close the created file objects
        '''
        if self.txt_path is not None:
            self.txt_file.close()
        if self.csv_path is not None:
            self.csv_file.close()
        if self.txt_loss_path is not None:
            self.txt_loss_file.close()
        if self.csv_loss_path is not None:
            self.csv_loss_file.close()
        if self.par_path is not None:
            self.par_file.close()
        if self.csv_percent_path is not None:
            self.csv_percent_file.close()
        if self.txt_percent_path is not None:
            self.txt_percent_file.close()
        if self.txt_best_path is not None:
            self.txt_best_file.close()
        if self.csv_best_path is not None:
            self.csv_best_file.close()
        if self.txt_rnd_decisions_path is not None:
            self.txt_rnd_decisions_file.close()
        if self.csv_rnd_decisions_path is not None:
            self.csv_rnd_decisions_file.close()

def plot_avg(csv_path, save_path, algorithm):
    ''' Read data from csv file and plot the results
    '''
    print("create figure avg")
    import matplotlib.pyplot as plt
    with open(csv_path) as csvfile:
        print(csv_path)
        reader = csv.DictReader(csvfile)
        xs = []
        ys = []
        for row in reader:
            xs.append(int(row['episode']))
            ys.append(float(row['steps']))
        fig, ax = plt.subplots()
        ax.plot(xs, ys, label=algorithm)
        ax.set(xlabel='episode', ylabel='Average Steps for Completion')
        ax.legend()
        ax.grid()

        save_dir = os.path.dirname(save_path)
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        fig.savefig(save_path)

def plot_best(csv_path, save_path, algorithm):
    ''' Read data from csv file and plot the results
    '''
    #print("create figure")
    import matplotlib.pyplot as plt
    with open(csv_path) as csvfile:
        print(csv_path)
        reader = csv.DictReader(csvfile)
        xs = []
        ys = []
        for row in reader:
            xs.append(int(row['episode']))
            ys.append(float(row['best']))
        fig, ax = plt.subplots()
        ax.plot(xs, ys, label=algorithm)
        ax.set(xlabel='episode', ylabel='Least Steps for Completion')
        ax.legend()
        ax.grid()

        save_dir = os.path.dirname(save_path)
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        fig.savefig(save_path)

def plot_dec(csv_path, save_path, algorithm):
    ''' Read data from csv file and plot the results
    '''
    #print("create figure")
    import matplotlib.pyplot as plt
    with open(csv_path) as csvfile:
        print(csv_path)
        reader = csv.DictReader(csvfile)
        xs = []
        ys = []
        for row in reader:
            xs.append(int(row['episode']))
            ys.append(float(row['random decisions in %']))
        fig, ax = plt.subplots()
        ax.plot(xs, ys, label=algorithm)
        ax.set(xlabel='episode', ylabel='Random Decisions in %')
        ax.legend()
        ax.grid()

        save_dir = os.path.dirname(save_path)
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        fig.savefig(save_path)

def plot_percent(csv_path, save_path, algorithm):
    ''' Read data from csv file and plot the results
    '''
    print("create percent figure")
    import matplotlib.pyplot as plt
    with open(csv_path) as csvfile:
        print(csv_path)
        reader = csv.DictReader(csvfile)
        xs = []
        ys = []
        for row in reader:
            xs.append(int(row['episode']))
            ys.append(float(row['percent']))
        fig, ax = plt.subplots()
        ax.plot(xs, ys, label=algorithm)
        ax.set(xlabel='episode', ylabel='Solving Accuracy in %')
        ax.legend()
        ax.grid()

        save_dir = os.path.dirname(save_path)
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        fig.savefig(save_path)

def plot_figures_one_percent(csv_pathes, save_path, algorithm_list):
    ''' Read data from csv files and plot the results
    '''
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()

    for i in range(len(csv_pathes)):
        csv_path = csv_pathes[i]
        algorithm = algorithm_list[i]

        print("csv path: ", csv_path)
        print("algorithm: ", algorithm)

        with open(csv_path) as csvfile:
            print(csv_path)
            reader = csv.DictReader(csvfile)
            xs = []
            ys = []
            for row in reader:
                xs.append(int(row['episode']))
                ys.append(float(row['percent']))

            ax.plot(xs, ys, label=algorithm)
            ax.set(xlabel='episode', ylabel='Solving Accuracy in %')
    ax.legend()
    #ax.grid()

    save_dir = os.path.dirname(save_path)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    fig.savefig(save_path)

def plot_figures_one_dec(csv_pathes, save_path, algorithm_list):
    ''' Read data from csv files and plot the results
    '''
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()

    for i in range(len(csv_pathes)):
        csv_path = csv_pathes[i]
        algorithm = algorithm_list[i]

        print("csv path: ", csv_path)
        print("algorithm: ", algorithm)

        with open(csv_path) as csvfile:
            print(csv_path)
            reader = csv.DictReader(csvfile)
            xs = []
            ys = []
            for row in reader:
                xs.append(int(row['episode']))
                ys.append(float(row['random decisions in %']))

            ax.plot(xs, ys, label=algorithm)
            ax.set(xlabel='episode', ylabel='Random Decisions in %')
    ax.legend()
    #ax.grid()

    save_dir = os.path.dirname(save_path)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    fig.savefig(save_path)

def plot_figures_one_avg(csv_pathes, save_path, algorithm_list):
    ''' Read data from csv files and plot the results
    '''
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()

    for i in range(len(csv_pathes)):
        csv_path = csv_pathes[i]
        algorithm = algorithm_list[i]

        print("csv path: ", csv_path)
        print("algorithm: ", algorithm)

        with open(csv_path) as csvfile:
            print(csv_path)
            reader = csv.DictReader(csvfile)
            xs = []
            ys = []
            for row in reader:
                xs.append(int(row['episode']))
                ys.append(float(row['steps']))

            ax.plot(xs, ys, label=algorithm)
            ax.set(xlabel='episode', ylabel='Average Steps for Completion')
    ax.legend()
    #ax.grid()

    save_dir = os.path.dirname(save_path)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    fig.savefig(save_path)
def plot_figures_one_best(csv_pathes, save_path, algorithm_list):
    ''' Read data from csv files and plot the results
    '''
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()

    for i in range(len(csv_pathes)):
        csv_path = csv_pathes[i]
        algorithm = algorithm_list[i]

        print("csv path: ", csv_path)
        print("algorithm: ", algorithm)
        print("save path: ", save_path)

        with open(csv_path) as csvfile:
            print(csv_path)
            reader = csv.DictReader(csvfile)
            xs = []
            ys = []
            for row in reader:
                xs.append(int(row['episode']))
                ys.append(float(row['best']))

            ax.plot(xs, ys, label=algorithm)
            ax.set(xlabel='episode', ylabel='Least Steps for Completion')
    ax.legend()
    #ax.grid()

    save_dir = os.path.dirname(save_path)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    fig.savefig(save_path)