import sys
import fileinput
from csv import reader
from functools import total_ordering

states_dict = {}
with open('/home/john/Documents/BowTieData/states.csv', 'r') as states_csv:
    r = reader(states_csv)
    next(r)
    for line in r:
        states_dict[line[0]] = line[1]

def translate_state(state: str) -> str:
    if state in states_dict:
        return states_dict[state]
    return state

@total_ordering
class Location():
    def __init__(self, id_num: int, street: str = None, city: str = None, \
                 state: str = None, postal_code: str = None, \
                 country: str = None):
        if not (street or city or state or postal_code or country):
            raise ValueError("Cannot create location without any information")
        self.id_num = id_num
        self.street = street and street.strip()
        self.city = city and city.strip()
        self.state = state and translate_state(state.strip())
        self.postal_code = postal_code and postal_code.strip()
        self.country = country and country.strip()

    def __str__(self):
        fields = [str(self.id_num), self.street, self.city, self.state, \
                  self.postal_code, self.country]
        fields = [s or "" for s in fields]
        fields = ['"' + s + '"' for s in fields]
        return ','.join(fields)
#        output = ""
#        output += ','.join(['"'+(s or "")+'"' for s in output])
#        output += '"' + (str(self.id_num)) + '",'
#        output += '"' + (self.street or "") + '",'
#        output += '"' + (self.city or "") + '",'
#        output += '"' + (self.state or "") + '",'
#        output += '"' + (self.postal_code or "") + '",'
#        output += '"' + (self.country or "") + '"'
#        return output

    def __key(self):
        return (self.street or "", self.city or "", self.state or "", \
                self.postal_code or "", self.country or "")

    def __eq__(self, other):
        return self.__key() == other.__key()

    def __hash__(self):
        return hash(self.__key())

    def __lt__(self, other):
        d1 = self.__dict__
        d2 = other.__dict__
        if count(d1) < count(d2):
            return True
        if other.street and (not self.street or (self.street < other.street)):
            return true
        if other.city and (not self.city or (self.city < other.city)):
            return true
        if other.state and (not self.state or (self.state < other.state)):
            return true
        if other.postal_code and (not self.postal_code or \
           (self.postal_code < other.postal_code)):
            return true
        if other.country and (not self.country or \
           (self.country < other.country)):
            return true
        return false

#old_locations = set()
old_locations = {}
#locations = set()
locations = {}
id_number = 0
locations_file_path = '/home/john/Documents/BowTieData/Modified/Locations.txt'

with open(locations_file_path, 'r') \
     as location_file:
    r = reader(location_file)
    next(r)
    for line in r:
        try:
            loc = Location(int(line[0]), line[1], line[2], line[3], line[4], \
                           line[5])
        except ValueError:
            continue
        if loc not in old_locations:
            id_number = max(id_number, int(line[0]))
#            old_locations.add(loc)
            old_locations[loc] = int(line[0])

location_indices = []
for index in sys.argv[3:8]:
    try:
        x = int(index)
        location_indices.append(x)
    except ValueError:
        location_indices.append(None)

with open(sys.argv[1], 'r') as input_file, \
     open(sys.argv[2], 'w') as output_file:
    r = reader(input_file)
    first_line = next(r)
    other_value_indices = []
    other_values = []
    for i, v in enumerate(first_line):
        if i not in location_indices:
            other_value_indices.append(i)
            other_values.append(v)
    other_values.append("LocationID")
    output_file.write(','.join(['"'+s+'"' for s in other_values]))
    output_file.write('\n')
    for line in r:
        id_number += 1
        street = location_indices[0] and line[location_indices[0]]
        city = location_indices[1] and line[location_indices[1]]
        state = location_indices[2] and line[location_indices[2]]
        postal_code = location_indices[3] and line[location_indices[3]]
        country = location_indices[4] and line[location_indices[4]]

        line_id = id_number
        try:
            loc = Location(id_number, street, city, state, postal_code, country)
            if loc in old_locations:
                line_id = old_locations[loc]
            elif loc in locations:
                line_id = locations[loc]
            else:
                locations[loc] = id_number
#            if loc not in old_locations and loc not in locations:
#                locations.add(loc)
        except ValueError:
            pass

        other_values = [line[i] for i in other_value_indices]
        other_values.append(str(line_id))
        output_file.write(','.join(['"'+s+'"' for s in other_values]))
        output_file.write('\n')

with open(locations_file_path, 'a') as location_file:
    for loc, id_ignore in sorted(locations.items(), key=lambda x: x[1]):
        location_file.write(str(loc))
        location_file.write('\n')


#!/usr/bin/python3
"""Upotreba ./validate3.py test/*.in*"""

import string

mjesec = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def solve(d, m, g):
  while 1:
    d += 1
    X = mjesec[m - 1]
    if g % 4 == 0 and m == 2: X += 1
    if d > X:
      d = 1
      m += 1
      if m > 12:
        m = 1
        if g == 9999:
          assert 0, "nema rjesenja"
        g += 1

    D = ""
    if d < 10: D = "0" + str(d)
    else: D = str(d)
    M = ""
    if m < 10: M = "0" + str(m)
    else: M = str(m)
    G = ""
    if g < 10: G = "000" + str(g)
    if g >= 10 and g < 100: G = "00" + str(g)
    if g >= 100 and g < 1000: G = "0" + str(g)
    if g > 1000: G = str(g)

    s = D + M + G
    s1 = s[::-1]
    if s == s1: return (d, m, g)
  assert 0, "tu nije trebalo doci"
  return (-1,-1,-1)

def check(lines):
    nl = []   # ispravno formatirane linije
    E = "\n"  # line ending

    n = int(lines[0])
    assert 1 <= n <= 10000, "krivi n"
    nl.append("{}{}".format(n, E))

    isti_mjesec = 1
    ista_god = 1
    for i in range(1, n + 1):
      s = lines[i].strip()
      assert len(s) == 11, "kriva duljina"
      assert s[2] == s[5] == s[-1] == '.', "krive tocke"
      nl.append("{}{}".format(s, E))
      d = int(s[0:2])
      m = int(s[3:5])
      g = int(s[6:-1])
      M = mjesec[m - 1]
      if g % 4 == 0 and m == 2: M += 1
      assert 1 <= d <= M, "krivi dan"
      assert 1 <= m <= 12, "krivi mjesec"
      assert 0 <= g <= 9999, "kriva godina"

      ans = solve(d, m, g)
      if ans[1] != m: isti_mjesec = 0
      if ans[2] != g: ista_god = 0

    assert lines == nl, "Krivi format (%s vs %s)" % (lines, nl)
    assert lines[-1][-1] == "\n", "Zadnji red ne zavrsava sa \\n"
    return {"n" : n, "mje" : isti_mjesec, "god" : ista_god}

# Ocekivani clusteri! Ovo vjerojatno zelis promijeniti!
expected_clusters = {"prvi" : 2, "drugi" : 2, "treci" : 4, "cet" : 2}

def what_cluster(data):
    # na temelju povratne informacije iz check(lines)
    # zakljucuje za TP u kojoj je bodovnoj sekciji
    if data['n'] == 10 and data['mje'] == 1: return "prvi"
    if data['n'] == 10 and data['god'] == 1: return "drugi"
    if data['n'] == 10: return "treci"
    return "cet"

################### Zadatak-specifican kod iznad ove linije #########################

import sys
import glob
import hashlib

def group_in_batches(files):
    # mnozenje.in.1a, mnozenje.in.1b sprema u isti batch

    files.sort()
    B = []
    for f in files:
        if f[-1].islower() and len(B) > 0 and f[:-1] == B[-1][-1][:-1]:
            B[-1].append(f)
        else:
            B.append([f])
    return B

if __name__ == "__main__":
    f = []
    for pattern in sys.argv[1:]:
        for filename in glob.glob(pattern):
            f.append(filename)

    bc = []
    for batch in group_in_batches(f):
        if 'dummy' not in batch[0]:
            bc.append([])
        for filename in batch:
            print("{}: ".format(filename), end="")
            try:
                lines = open(filename).readlines()
                summary = check(lines)
                c = what_cluster(summary)
                if 'dummy' not in batch[0]:
                    bc[-1].append(c)
            except Exception as e:
                print("Greska!", e)
                raise
            else:
                print("Sve ok! (cluster {}, summary = {})".format(c, summary))

    clusters = {}
    for b in bc:
        for c in b:
            assert c == b[0], "Ima razlicitih cluster-a unutar batcha"
        if not b[0] in clusters:
            clusters[b[0]] = 0
        clusters[b[0]] += 1

    assert clusters == expected_clusters, "Kriva raspodjela clustera ({} vs {})".format(clusters, expected_clusters)

    # Buda test - provjeri duplikate
    # hashes = set(hashlib.sha1(open(x, 'rb').read()).hexdigest() for x in f)
    # assert len(hashes) == len(f), "Ima duplikata!"


#
# Tests the entity class bindings
#
import unittest

class EntityTestCase(unittest.TestCase):

    def test_create(self):
        from libcellml.entity import Entity

        self.assertRaises(AttributeError, Entity)

    def test_id(self):
        from libcellml import Model

        # std::string id()
        x = Model()
        self.assertEqual(x.id(), '')

        # void setId(const std::string &id)
        idx = 'test'
        x.setId(idx)
        self.assertEqual(x.id(), idx)

    def test_parent(self):
        from libcellml import Model, Component

        x = Model("model")
        self.assertIsNone(x.parent())

        c = Component()
        self.assertIsNone(c.parent())
        c.setParent(x)
        self.assertEqual("model", c.parent().name())
        self.assertEqual(0, x.componentCount())

    def test_remove_parent(self):
        from libcellml import Model, Component

        m = Model()
        c = Component()
        c.setParent(m)
        self.assertIsNotNone(c.parent())
        c.removeParent()
        self.assertIsNone(c.parent())

    def test_has_parent(self):
        from libcellml import Component

        # bool hasParent(Component* c)
        x = Component()
        c1 = Component()
        self.assertFalse(x.hasParent())
        x.setParent(c1)
        self.assertTrue(x.hasParent())
        x.removeParent()
        self.assertFalse(x.hasParent())
        c2 = Component()
        c2.setParent(c1)
        x.setParent(c2)
        self.assertTrue(x.hasParent())

    def test_has_ancestor(self):
        from libcellml import Component

        # bool hasParent(Component* c)
        x = Component()
        c1 = Component()
        c2 = Component()
        self.assertFalse(c2.hasAncestor(c1))
        self.assertFalse(c2.hasAncestor(x))

        c1.setParent(x)
        c2.setParent(c1)
        self.assertTrue(c2.hasAncestor(c1))
        self.assertTrue(c2.hasAncestor(x))

class NamedEntityTestCase(unittest.TestCase):

    def test_create(self):
        from libcellml.namedentity import NamedEntity

        self.assertRaises(AttributeError, NamedEntity)

class LoggerTestCase(unittest.TestCase):

    def test_create(self):
        from libcellml.logger import Logger

        self.assertRaises(AttributeError, Logger)

    def test_issue_types(self):
        from libcellml import Parser, Issue

        p = Parser()

        i1 = Issue()
        i1.setLevel(Issue.Level.ERROR)
        i1.setDescription("error error")
        p.addIssue(i1)

        i4 = Issue()
        i4.setLevel(Issue.Level.WARNING)
        i4.setDescription("warning warning")
        p.addIssue(i4)

        i2 = Issue()
        i2.setLevel(Issue.Level.HINT)
        i2.setDescription("hint hint")
        p.addIssue(i2)

        i3 = Issue()
        i3.setLevel(Issue.Level.MESSAGE)
        i3.setDescription("message message")
        p.addIssue(i3)

        self.assertEqual(1, p.errorCount())
        error = p.error(0)
        self.assertEqual("error error", error.description())

        self.assertEqual(1, p.warningCount())
        warning = p.warning(0)
        self.assertEqual("warning warning", warning.description())

        self.assertEqual(1, p.hintCount())
        hint = p.hint(0)
        self.assertEqual("hint hint", hint.description())

        self.assertEqual(1, p.messageCount())
        message = p.message(0)
        self.assertEqual("message message", message.description())

        self.assertEqual(4, p.issueCount())
        p.removeAllIssues()
        self.assertEqual(0, p.issueCount())

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


#The process of making a motion detection app is as below:
#We first capture the video using the video cam, and blur it.
#Then, we write out a delta frame, using the background frame and the current frame from the webcam.
#Then, we create a threshold frame, coloring the moving object in white.
#Then, we look for a contour, bigger than a certain number, and create a rectangle based on it.
#We apply the rectangle to the colored catputure, and viola, you got a motion dector.
#------------------

import cv2, time

video=cv2.VideoCapture(0)

first_frame=None
second_time=0
#we will need the first frame of the caputure, so that we can diferentiate a moving object from the background.
#So, we will create a new object, and asign 'None' so that python wont throw errors saying first_frame is not defined.

while True:
    check, frame= video.read()

    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    gray=cv2.GaussianBlur(gray,(25,25),0)
    #we blur the capture a little bit to get rid of some noise.
        #1: the object which you want to blur
        #2: the blurryness (see the documentation for details)
        #3: standard deviation (see the documentation fro details)

    if second_time !=100:
        first_frame=gray
        second_time=second_time+1
        #Since the 'first_frame' would be 'None' only the first time the while loop runs, we pair it with a conditioning statement
        continue
        #This means to continue to the beginning of the while loop.
        #we dont want to do any processing with the first frame so we end it here.

    delta_frame=cv2.absdiff(first_frame,gray)
    #creates the delta frame.
    #1,2: The two object that you want to see the difference of.

    thresh_frame=cv2.threshold(delta_frame, 30,255,cv2.THRESH_BINARY)[1]
    #creates a black and white catpture, displaying places with more than 30 pixel in the delta_frame as black

    thresh_frame=cv2.dilate(thresh_frame, None, iterations=2)
    #dilates the image, so that the blacked out pixels are smooth

    (_,cnts,_)=cv2.findContours(thresh_frame.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    #this looks for any contours in the threshold_frame, and saves it in a tuple

    for contour in cnts:
        if cv2.contourArea(contour) < 10000:
            continue
        (x,y,w,h)=cv2.boundingRect(contour)
        cv2.rectangle(frame, (x,y),(x+w,y+h),(0,255,0),3)
        #if the contour is big enough, it is converted in to a rectangle, and the coordinates are passed to '(x,y,w,h)', to draw a rectangle cv2.rectangle

    cv2.imshow("Gray Frame", gray)
    cv2.imshow("Delta Frame", delta_frame)
    cv2.imshow('Threshold Frame',thresh_frame)
    cv2.imshow('Capture',frame)
    print(delta_frame)
    #Numbers will rise where there is movement.

    key=cv2.waitKey(1)

    if key==ord('q'):
        break

video.release()
cv2.destroyAllWindows()


#Creado por Mario Lara Molina
#Fecha de creación 05/08/2020 7:40 am
#Última modficacion 05/08/2020
#Versión 3.8.3
#Definición de funciones
def determinarMenAux(cifra,men=9):
    if cifra==0:
        return men
    else:
        if (cifra%10)< men:
            men=(cifra%10)
        return determinarMenAux(cifra//10,men)
def determinarMen(cifra):
    try:
        if not isinstance(cifra,int):
            return "La cifra debe de ser de tipo entero"
        elif  cifra<0:
            return "La cifra debe ser mayor  a cero"
        else:
            return determinarMenAux(cifra,men=9)
    except:
        return "Ingrese un dato de tipo entero"


'''Version with 2-dim observation space'''

import gym
import numpy as np
from gym import spaces
import pandas as pd
import math
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random

def encode_wealth(wealth, wealth_bins):
    '''Encodes the wealth from [0, +Inf) into the corresponding wealth_bin and returns the wealth_bin encoded as integer'''
    return(pd.cut(x=[wealth], bins=wealth_bins, right=False, labels=False)[0])

def encode_action(action, actions):
    '''Encodes an action as the corresponding index in actions'''
    return(int(np.where(action == actions)[0][0]))

def decode_action(action, actions):
    '''Decodes the index as the corresponding investment in the risky asset'''
    return(actions[action])

class BinomialTree(gym.Env):
    '''Custom binomial stock price tree environment with one risky-asset and bank account'''
    metadata = {'render.modes': ['human']}

    def __init__(self, up_prob, up_ret, down_ret, r, T, dt, V_0, actions, utility):
        assert divmod(T, dt)[1] == 0        # To-Do: change to ValueError, is T 'ganzzahlig' divisible
        super().__init__()

        self.probs      = np.array([up_prob, 1-up_prob])    # probabilities for an upward/downward step of the risky asset
        self.returns    = np.array([up_ret, down_ret])      # risky asset return for an upward/downward step
        self.r = r                                          # risk-free rate (bank account return, riskless)
        self.T = T                                          # Termination time
        self.dt = dt                                        # time-step size
        self.num_timesteps = T//dt
        self.V_0 = V_0                                      # Initial wealth
        self.actions = actions                              # possible actions, fraction of wealth invested in risky aset
        self.num_actions = len(self.actions)                # number of possible actions
        #self.wealth_bins = wealth_bins                      # discrete wealth space
        self.utility = utility                              # "log" or "sqrt"

        self.reset()                                        # Set environment to initial state (0, V_0)

        # Action space
        self.action_space = spaces.Discrete(self.num_actions)

        # Observation space np.array([t, V_t])
        self.observation_space = spaces.Box(low = np.array([0, 0]),
                                            high = np.array([self.T, float("inf")]),
                                            shape = (2,),
                                            dtype = np.float32)

    def step(self, action):
        '''Execute one time step within the environment'''

        """
        Samples at each time step from the vector of returns [up_ret, down_ret] with probabilities p=[up_prob, 1-up_prob], and calculates
        the new wealth according to the investment in the riskless asset and the risky asset.
        """

        assert self.action_space.contains(action)

        # Decode the action
        pi_t = decode_action(action, self.actions)

        # Take one step according to binomial tree dynamics
        self.V_t *= pi_t * self.dt * (np.random.choice(a=self.returns, size=1, replace=False, p=self.probs)[0] - self.r) + (1 + self.dt*self.r)  # Update Wealth (see notes)

        # Update time-step
        self.t += self.dt

        done = self.t == self.T                       # Episode is finished if termination time is reached

        reward = 0                                             # Reward is zero for each time step t<T
        if done:                                               # Reward at termination time R_T = U(V_T)
            if self.utility == "log":                              # Logarithmic utility function
                reward = np.log(self.V_t)
            elif self.utility == "sqrt":                           # Square root utility function
                reward = np.sqrt(self.V_t)
            else:
                raise ValueError("Utility function {} not implemented.".format(self.utility))

        return self._get_obs(), reward, done, {}          # {} empty info

    def _get_obs(self):
        '''Get observation from environment'''
        return np.array([self.t, self.V_t])

    def reset(self):
        '''Reset the state of the environment to an initial state'''
        self.t   = 0                                          # setting time to zero
        self.V_t = self.V_0                                   # setting wealth to V_0
        return self._get_obs()

    #def render(self, mode='human', close=False):
    # Render the environment to the screen
    #...

def fun(x, y, Q, actions):
    '''Help function used in plot_q_values'''
    # Returns the Q-values for each state-action pair at time step 1.
    return np.array([Q[(1,wealth)][encode_action(action, actions)] for action, wealth in zip(x,y)])

def plot_q_values(Q, actions):
    '''Creates a 3d Wireframe plot of the Q-value function for each state-action pair and adds the predicted action (i.e. argmax_a Q(s,a)'''
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    x = actions
    y = np.array(sorted([wealth for t, wealth in Q.keys() if t == 1]))
    X, Y = np.meshgrid(x, y)
    zs = np.array(fun(np.ravel(X), np.ravel(Y), Q, actions))
    Z = zs.reshape(X.shape)

    # Predicted Actions for each state
    states = [key for key in Q.keys() if key[0] == 1]
    predicted_actions = [decode_action(np.argmax(Q[state]), actions) for state in states]
    wealths = [wealth for _, wealth in states]
    predicted_Q_values = [Q[state][np.argmax(Q[state])] for state in states]

    ax.plot_wireframe(X, Y, Z, color="black")

    ax.set_xlabel('investment in risky asset')
    ax.set_ylabel('wealth')
    ax.set_zlabel('Q-values')
    ax.scatter(predicted_actions, wealths, predicted_Q_values, zdir="z", c="red", alpha=1, label="Predicted Actions")
    plt.title("Learned Q-value surface")
    ax.legend()

    plt.show()


from MCTS.node import Node
from time import time

class Tree:
    def __init__(self, root_state, actor, critic):
        self.root = Node(None, root_state)
        self.actor = actor
        self.critic = critic

    def mcts(self, start_time, get_next_state, c, rollout_prob):
        """
        Runs Monte Carlo Tree Search from the current root node.
        When the search has terminated, the best child of the root is selected as the new root, and the branches of the
        tree that are now unreachable are pruned.
        Returns the number of visits to each child node, the total number of visits, and the action that leads to the
        best (the most visited) child.
        """
        if self.root.children is None:
            self.root.expand(get_next_state)
        node = self.root
        while time() - start_time < 1:                          # run as many simulations as possible within 1 second
            while node.children and len(node.children) > 0:                 # traverse tree to find a leaf node
                node = node.best_child(c)
            if node.number_of_visits == 1:                                  # if leaf node is visited before
                node.expand(get_next_state)                                     # expand node
                if len(node.children) > 0:
                    node = node.children[0]
            value = node.rollout(self.actor, self.critic, rollout_prob)     # do rollout from leaf node
            while node.parent is not None:                                  # back-propagate values
                node = node.parent
                node.value += value
                node.number_of_visits += 1

        visits_dict, total_visits, best_child, best_action = self.root.children_visits()

        self.root = best_child
        self.root.parent = None

        return visits_dict, total_visits, best_action


import sys
from logging.config import fileConfig

from alembic import context
from sqlalchemy import engine_from_config
from sqlalchemy import pool

sys.path = ['', '..'] + sys.path[1:]

from config.db import db_url
from config.base import Base

config = context.config

fileConfig(config.config_file_name)

target_metadata = Base.metadata

# target_metadata = None

# my_important_option = config.get_main_option("my_important_option")
# ... etc.

def run_migrations_offline():

    # url = config.get_main_option("sqlalchemy.url")
    url = db_url
    context.configure(
        url=url,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
    )

    with context.begin_transaction():
        context.run_migrations()

def run_migrations_online():

    configuration = config.get_section(config.config_ini_section)
    configuration['sqlalchemy.url'] = db_url
    connectable = engine_from_config(
        configuration,
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
    )

    with connectable.connect() as connection:
        context.configure(
            connection=connection, target_metadata=target_metadata
        )

        with context.begin_transaction():
            context.run_migrations()

if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()


"""Solution to an exercise in Think Python."""

"Author:"
"Ambika Goel"

def grid(a,b,c,d,g,l):
	e=c+((b+c)*g)+a # prints ' - - - - - +'
	f=c*(g*2+1)+d	# prints '           |'
	h='\n'		# starts new line
	i=a+e*2		# prints'+ - - - - - - + - - - - - - +'
	j=d+f*2		# prints '|             |             |'
	k=i+(h+j)*(l)+h # prints line i, then line j (l) times (basically top 1/2 				  of box)
	print (k)*2+i
grid('+','-',' ','|',1,1)


import numpy as np
import os
from .EventStream import EventStream
from .config      import VERSION, TYPE

AMDtype=[('x',np.uint8), ('y', np.uint8), ('s', np.uint8), ('int', np.uint8), ('ts', np.uint64)]

class AMDStream(Stream):
    """
    """
    def __init__(self, _events, _version=VERSION):
        super().__init__(_events, AMDtype, _version);

    def write(self, filename):
        """
        """
        if not os.path.exists(os.path.dirname(filename)):
            try:
                os.makedirs(os.path.dirname(filename))
            except OSError as exc: # Guard against race condition
                if exc.errno != errno.EEXIST:
                    raise

        to_write = bytearray('Event Stream','ascii');
        to_wrtie.append(self.version.split('.')[0]);
        to_wrtie.append(self.version.split('.')[1]);
        to_wrtie.append(self.version.split('.')[2]);
        to_write.append(TYPE['AMD']);
        previousTs = 0;
        for datum in self.data :
            relativeTs = datum.ts - previousTs;
            if relativeTs > 253:
                numberOfOverflows = int(relativeTs / 254);
                for i in range (numberOfOverflows):
                    to_write.append(0xff);
                relativeTs -= numberOfOverflows * 254;
            to_write.append(np.uint8(relativeTs));
            to_write.append(np.uint8(datum.x));
            to_write.append(np.uint8(datum.y));
            to_write.append(np.uint8(datum.s));
            previousTs = datum.ts;
        ES_file = open(filename, 'wb');
        ES_file.write(to_write);
        ES_file.close();


import gdb
import sys
import json
from collections import OrderedDict
import re

functions = [
    "commit_creds",
    "arch_jump_label_transform",
    "module_param_sysfs_setup",
    "module_param_sysfs_remove",
    "wake_up_new_task",
    "flush_old_exec",
    "do_exit",
    "arch_ptrace",
    "compat_arch_ptrace",
    "process_vm_rw_core",
    "__vma_link_rb",
    "change_protection",
    "vma_adjust",
    "__vma_adjust",
    "vma_rb_erase",
    "__vma_rb_erase",
    "expand_downwards",
    "complete_signal",
    "text_poke",
    "__text_poke",
    "ftrace_write",
    "panic",
    "crash_kexec",
    "__access_remote_vm",
    "mprotect_fixup",
    "do_munmap",
    "vma_adjust",
]

class Offsets(gdb.Command):
    def __init__(self):
        super (Offsets, self).__init__ ('all-offsets', gdb.COMMAND_DATA)

    def dump_struct(self, struct, fields=[], parent_offset=0):
        for field in struct.fields():
            offset  = parent_offset + field.bitpos // 8
            if not field.name:
                self.dump_struct(field.type, fields, offset)
            else:
                fields[field.name] = offset

    def get_fields(self, name):
        mbrs = OrderedDict()
        st = None
        st = gdb.lookup_type(name)
        self.dump_struct(st, fields=mbrs)
        mbrs["sizeof"] = st.sizeof
        return mbrs

    def invoke(self, arg, from_tty):
        argv = gdb.string_to_argv(arg)
        if len(argv) != 1:
            raise gdb.GdbError("Please specify the output file.")

        structs = OrderedDict()

        names = gdb.execute("info types", to_string=True)

        rx = re.compile("struct [a-zA-Z_][A-Za-z0-9_]*")
        structs = rx.findall(names)
        print(len(structs))

        output = {}
        for struct in structs:
            try:
                output[struct] = self.get_fields(struct)
            except Exception as e:
                print("Exception for [%s]. Will skip: " % struct, file=sys.stderr)
                print(e, file=sys.stderr)
                print("Will skip...", file=sys.stderr)

        ver = gdb.execute("p linux_banner", to_string=True)

        rx = re.compile("[0-9]+\.[0-9]+\.[0-9]+[^ ]*")
        ver = rx.findall(ver)
        output["_KERNEL_VERSION_"] = ver[0] + "*"

        output["_FUNCTIONS_"] = []

        for func in functions:
            try:
                out = gdb.execute("whatis " + func)
                output["_FUNCTIONS_"].append(func)
            except:
                pass

        with open(argv[0], "w") as outfile:
            json.dump(output, outfile, indent=4)

        return

Offsets()


import Tkinter
import Tkconstants
import tkFont

from pomodoro import Pomodoro

class App:
    def __init__(self, master):
        self.master=master

        font = tkFont.Font(family="Helvetica", size=50, weight=tkFont.BOLD)
        self.pomodoro_count_text = Tkinter.StringVar()
        Tkinter.Label(master, textvariable=self.pomodoro_count_text, justify=Tkconstants.CENTER).grid(row=0, columnspan=2)
        self.time_left = Tkinter.StringVar()
        Tkinter.Label(master, textvariable=self.time_left, font=font, width=5).grid(row=1, columnspan=2, sticky=Tkconstants.W)

        self.start_text = Tkinter.StringVar()
        self.start_button = Tkinter.Button(master, textvariable=self.start_text, command=self.start_clicked)
        self.reset_button = Tkinter.Button(master, text="Reset", command=self.reset_pomodoro_count)

        self.start_button.grid(row=2,column=0)
        self.reset_button.grid(row=2, column=1)

        self.reset_pomodoro_count()
        self.reset_time_left()
        self.start_text.set("Start")

    def update_pomodoro_count(self):
        self.pomodoro_count_text.set("%d completed" % self._pomodoro_count)
        if self._pomodoro_count == 0:
            self.reset_button.config(state = Tkconstants.DISABLED)
        else:
            self.reset_button.config(state = Tkconstants.ACTIVE)

    def increase_pomodoro_count(self):
        self._pomodoro_count += 1
        self.update_pomodoro_count()

    def reset_pomodoro_count(self):
        self._pomodoro_count = 0
        self.update_pomodoro_count()

    def reset_time_left(self):
        self.time_left.set("00:00")
        self.master.wm_title("Pomodoro")

    def start_clicked(self):
        self.start_text.set("Cancel")
        pomodoro = Pomodoro()
        stoppable = pomodoro.start(self.on_pomodoro_step, self.on_pomodoro_end)
        self.start_button.configure(command=lambda: self.cancel_clicked(stoppable))

    def on_pomodoro_step(self, min_left, sec_left):
        self.time_left.set("%02d:%02d" % (min_left, sec_left))
        self.master.wm_title(self.time_left.get())

    def broadcast_time_left(self, min_left, sec_left):
        self.pidgin.set_busy("Available in %s (Pomodoro)" % self.time_left.get())
        if not min_left % 5 == 0:
            return
        if min_left == 0:
            self.alerter.alert("Break!")
        else:
            self.alerter.alert("%d min left" % min_left)

    def on_pomodoro_end(self, completed):
        self.start_text.set("Start")
        self.start_button.configure(command=self.start_clicked)
        self.reset_time_left()
        if completed:
            self.increase_pomodoro_count()

    def cancel_clicked(self, stoppable):
        stoppable.stop()

def main():
    root = Tkinter.Tk()
    root.wm_title("Pomodoro")
    app = App(root)
    root.mainloop()

if __name__=="__main__":
    main()


import unittest
from groupKey import groupKeyVal

class Test1(unittest.TestCase):
    def testGroupKeyValMethod(self):
        self.assertEqual(groupKeyVal('input.txt'),
                         {'1': ['2', '5'], '2': ['3', '6'], '3': ['4', '7']})

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


from time import sleep
from random import uniform
from datetime import datetime
from os.path import getsize
from traceback import format_exc
from json import load, dump

from selenium import webdriver
from lxml import html
from fake_useragent import UserAgent
options = webdriver.ChromeOptions()
#options.add_argument('--headless')
options.add_argument(f'--user-agent={UserAgent().random}')

driver = webdriver.Chrome('./chromedriver', options=options)

main_url = 'https://www.walmart.com'
site_name = 'walmart'
search_term = 'Disposable Masks'

driver.get(main_url)
driver.find_element_by_id('global-search-input').send_keys(search_term+'\ue007')

sleep(uniform(.5,3.1))

tree = html.fromstring(driver.page_source)
products = tree.xpath('//li[@id="ProductTileGridView"]')

file_name = site_name + '_' + search_term.replace(' ', '_') + '_prices.json'

products_list = list()

with open(file_name, 'a+') as f:
    try:
        #fieldnames = ['name', 'price', 'review_score', 'num_reviews', 'per_cnt_price', 'url', 'timestamp']

        #csv_writer = csv.DictWriter(file, fieldnames=fieldnames)
        #csv_writer.writeheader()

        f.seek(0)

        if getsize(file_name) != 0:
            products_list = load(f)

        for i in range(20):

            if i != 0:
                sleep(uniform(.2, .9))
                driver.find_element_by_xpath('//button[@class="elc-icon paginator-hairline-btn paginator-btn paginator-btn-next"]::before').click()
                sleep(uniform(.4, 1.9))
                tree = html.fromstring(driver.page_source)
                products = tree.xpath('//li[@id^="ProductTileGridView"]')

            for product in products:
                product_dict = dict()

                product_name = str(product.xpath('.//a[@data-type="itemTitles"]')[0])
                timestamp = str(datetime.now()).split('.')[0]

                existing_product_list = [d for d in products_list if d['name'] == product_name]

                if len(existing_product_list) > 0:
                    product_dict = existing_product_list[0]

                    #print(list([k.split(' ')[0] for k, v in product_dict['time_based'].items()]))
                    if timestamp.split(' ')[0] in [k.split(' ')[0] for k, v in product_dict['time_based'].items()]:
                        #print('skipped')
                        #print(timestamp.split(' ')[0])
                        #print('\n')
                        continue

                else:
                    products_list.append(product_dict)

                    product_dict['name'] = product_name

                    product_dict['url'] = str(main_url + product.xpath('.//a[@data-type="itemTitles"]/@href')[0])

                    product_dict['time_based'] = dict()
                    product_dict['time_based'][timestamp] = dict()

                time_based_dicts = product_dict['time_based'][timestamp]

                time_based_dicts['price'] = str(''.join(product.xpath('.//span[@class="price-group"]/text()')))

                per_cnt_price = product.xpath('.//span[@class="a-size-base a-color-secondary"]/text()')
                time_based_dicts['per_cnt_price'] = str(per_cnt_price[0]) if len(per_cnt_price) > 0 else '-'

                review_score = product.xpath('.//span[@class="average-rating"]/span[@class="visuallyhidden seo-avg-rating"]/text()')
                time_based_dicts['review_score'] = str(review_score[0]) if len(review_score) > 0 else '-'

                num_reviews = product.xpath('.//span[@class="start-reviews-count"]/span/text()')
                time_based_dicts['num_reviews'] = str(num_reviews[0]) if len(num_reviews) > 0 else '0'

    except Exception:
        print(format_exc())

with open(file_name, 'w') as f:
    dump(products_list, f, indent=4)

driver.quit()


# Скрипт для установки уровней/Script for downloading levels
import os # импортируем модуль для использования терминала

# функция скачивания уровней(ня)
def downloading():
    repesitory = input('Github репозиторий(Github Repository) : ') # справшиваем репезиторий с уровнями с github.com
    os.system('git clone ' + repesitory) # скачиваем уровни

# если заупускают из этого файла, выполняем функцию скачивания
if __name__ == '__main__':
    downloading() # скачиваем


def merge_inplace(data, printer=None, offset=0):
    length = len(data)
    if length > 1:
        mid = length // 2
        left = data[:mid]
        right = data[mid:]

        if printer:
            printer(data, offset=offset)

        merge_inplace(left, printer=printer, offset=offset)
        merge_inplace(right, printer=printer, offset=offset+mid)

        if printer:
            printer(left+right, highlight=range(length), offset=offset)

        left_index, right_index = 0, 0
        data_index = 0

        left_length = len(left)
        right_length = len(right)

        while left_index < left_length and right_index < right_length:
            if left[left_index] < right[right_index]:
                data[data_index] = left[left_index]
                left_index += 1
            else:
                data[data_index] = right[right_index]
                right_index += 1
            data_index += 1
            if printer:
                printer(data[:data_index], highlight=range(length),
                        colour=(data_index-1, ), offset=offset)

        while left_index < left_length:
            data[data_index] = left[left_index]
            left_index += 1
            data_index += 1
            if printer:
                printer(data[:data_index], highlight=range(length),
                        colour=(data_index-1, ), offset=offset)

        while right_index < right_length:
            data[data_index] = right[right_index]
            right_index += 1
            data_index += 1
            if printer:
                printer(data[:data_index], highlight=range(length),
                        colour=(data_index-1, ), offset=offset)

        printer(data, offset=offset)

def merge_pure(data):
    if len(data) > 1:
        mid = len(data) // 2
        left = data[:mid]
        right = data[mid:]

        left = merge_pure(left)
        right = merge_pure(right)

        _data = []
        while left and right:
            if left[0] < right[0]:
                _data.append(left.pop(0))
            else:
                _data.append(right.pop(0))

        _data.extend(left)
        _data.extend(right)

        return _data
    return data


from bs4 import BeautifulSoup
import urllib
import re, string, datetime
import pandas as pd

def make_soup(url, tag, css_class):
	"""
	This function takes a url and gets the elements specified by tag with an optional
	css class specified
	"""

	# this is pretty standard for BeautifulSoup
	response = urllib.request.urlopen(url)
	html = response.read()
	soup = BeautifulSoup(html)

	# make css_class optional
	if css_class == None:
		element = soup(tag)
	else:
		element = soup(tag, {'class' : css_class})

	return element

def process_box_score_table(table):

	header_row = table('tr')[0]
	trs = table('tr')[1:]
	headers = []
	for th in header_row('th'):
		headers.append(th.text)

	df = pd.DataFrame(columns=headers,index=range(len(trs)))
	for i,tr in enumerate(trs):
		for j,td in enumerate(tr('td')):
			df.loc[i,headers[j]] = td.text

	return df

def process_play_by_play_table(table):

	table_head = table('thead')[0]
	pbp_headers = []
	head_row = table_head('tr')[0]
	for th in head_row('th'):
		pbp_headers.append(th.text)

	table_body = table('tbody')[0]
	pbp_rows = table_body.findAll(id=re.compile('event_[0-9]+'))
	pbp_df = pd.DataFrame(columns=pbp_headers,index=range(len(pbp_rows)))
	for i,row in enumerate(pbp_rows):
		for j,td in enumerate(row('td')):
			pbp_df.loc[i,pbp_headers[j]] = td.text

	return pbp_df

def process_game(tables):

	away_hit = process_box_score_table(tables[0])
	home_hit = process_box_score_table(tables[1])
	away_pitch = process_box_score_table(tables[2])
	home_pitch = process_box_score_table(tables[3])

	# can clean this up
	starting_lineups = process_box_score_table(tables[4])

	pbp_df = process_play_by_play_table(tables[5])
	return away_hit,away_pitch,home_hit,home_pitch,starting_lineups,pbp_df

def process_date(m,d,Y):

	day_url = 'http://www.baseball-reference.com/games/standings.cgi?month=%d&day=%d&year=%d' % (m,d,Y)
	day_response = urllib.request.urlopen(day_url)
	day_html = day_response.read()

	url_suffix_list = re.findall('/boxes/[\S]+\.shtml',str(day_html))
	urls = ['http://www.baseball-reference.com%s' % suffix for suffix in url_suffix_list]

	for url in urls:

		try:

			response = urllib.request.urlopen(url)
			html = response.read()
			soup = BeautifulSoup(html)

			tables = make_soup(url, 'table', css_class='sortable stats_table')

			away_hit,away_pitch,home_hit,home_pitch,starting_lineups,pbp_df = \
				process_game(tables)

			away_team_match = re.match('(.+)batting',tables[0]['id'])
			away_team = away_team_match.group(1)
			home_team_match = re.match('(.+)batting',tables[1]['id'])
			home_team = home_team_match.group(1)

			date_str = '%d%d%d' % (m,d,Y)

			away_hit.to_csv('baseball_reference_files\\%s_at_%s_%s_hitting_%s.csv' % (away_team,home_team,away_team,date_str),sep='|')
			away_pitch.to_csv('baseball_reference_files\\%s_at_%s_%s_pitching_%s.csv' % (away_team,home_team,away_team,date_str),sep='|')
			home_hit.to_csv('baseball_reference_files\\%s_at_%s_%s_hitting_%s.csv' % (away_team,home_team,home_team,date_str),sep='|')
			home_pitch.to_csv('baseball_reference_files\\%s_at_%s_%s_pitching_%s.csv' % (away_team,home_team,home_team,date_str),sep='|')

			starting_lineups.to_csv('baseball_reference_files\\%s_at_%s_lineups_%s.csv' % (away_team,home_team,date_str),sep='|')
			pbp_df.to_csv('baseball_reference_files\\%s_at_%s_pbp_%s.csv' % (away_team,home_team,date_str),sep='|')

		except:

			with open('error_log.txt','a') as file:
				file.write('error with %s' % url)

if __name__ == '__main__':

	# example url:
	# http://www.baseball-reference.com/games/standings.cgi?month=07&day=21&year=2015
	# http://www.baseball-reference.com/boxes/CHA/CHA201507210.shtml

	### need to add loop to process date to iterate over all url suffixes found

	for year in range(2001,2015):
		dates = pd.date_range(datetime.datetime(year,3,1),datetime.datetime(year,11,15))
		for date in dates:
			print('Processing %s...' % str(date))
			process_date(date.month,date.day,date.year)


import sys
sys.path.append('../..')

import RPi.GPIO as GPIO
import time
import pygame
from GameManager.MediaManager import MediaManager
from pprint import pprint

GPIO.setmode(GPIO.BCM)
# button # 1
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# button # 2
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# button # 3
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# button # 4
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# button # 5
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# button # 6
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)

media_manager = MediaManager()

while True:
    input_1 = GPIO.input(17)
    input_2 = GPIO.input(27)
    input_3 = GPIO.input(22)
    input_4 = GPIO.input(5)
    input_5 = GPIO.input(6)
    input_6 = GPIO.input(13)

    if input_1 == False:
        print('1 Pressed')
        media_manager.play("../../../media/button_1.mp3")
        time.sleep(1)

    if input_2 == False:
        print('2 Pressed')
        media_manager.play("../../../media/button_2.mp3")
        time.sleep(0.2)

    if input_3 == False:
        print('3 Pressed')
        media_manager.play("../../../media/button_3.mp3")
        time.sleep(0.2)

    if input_4 == False:
        print('4 Pressed')
        media_manager.play("../../../media/button_4.mp3")
        time.sleep(0.2)

    if input_5 == False:
        print('5 Pressed')
        media_manager.play("../../../media/button_5.mp3")
        time.sleep(0.2)

    if input_6 == False:
        print('6 Pressed')
        media_manager.play("../../../media/button_6.mp3")
        time.sleep(0.2)


# -------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# -------------------------------------------------------------

from typing import (TYPE_CHECKING, Any, Collection, Dict, KeysView, List,
                    Optional, Tuple, Union)

from py4j.protocol import Py4JNetworkError
from py4j.java_collections import JavaArray
from py4j.java_gateway import JavaGateway, JavaObject
from systemds.script_building.dag import DAGNode, OutputType
from systemds.utils.consts import VALID_INPUT_TYPES

if TYPE_CHECKING:
    # to avoid cyclic dependencies during runtime
    from systemds.context import SystemDSContext

class DMLScript:
    """DMLScript is the class used to describe our intended behavior in DML. This script can be then executed to
    get the results.
    """
    sds_context: 'SystemDSContext'
    dml_script: str
    inputs: Dict[str, DAGNode]
    prepared_script: Optional[Any]
    out_var_name: List[str]
    _variable_counter: int

    def __init__(self, context: 'SystemDSContext') -> None:
        self.sds_context = context
        self.dml_script = ''
        self.inputs = {}
        self.prepared_script = None
        self.out_var_name = []
        self._variable_counter = 0

    def add_code(self, code: str) -> None:
        """Add a dml code line to our script

        :param code: the dml code line
        """
        self.dml_script += code + '\n'

    def add_input_from_python(self, var_name: str, input_var: DAGNode) -> None:
        """Add an input for our preparedScript. Should only be executed for data that is python local.

        :param var_name: name of variable
        :param input_var: the DAGNode object which has data
        """
        self.inputs[var_name] = input_var

    def execute(self) -> JavaObject:
        """If not already created, create a preparedScript from our DMLCode, pass python local data to our prepared
        script, then execute our script and return the resultVariables

        :return: resultVariables of our execution
        """
        # we could use the gateway directly, non defined functions will be automatically
        # sent to the entry_point, but this is safer

        try:
            self.__prepare_script()
            ret = self.prepared_script.executeScript()
            return ret
        except Py4JNetworkError:
            exception_str = "Py4JNetworkError: no connection to JVM, most likely due to previous crash or closed JVM from calls to close()"
            trace_back_limit = 0
        except Exception as e:
            exception_str = str(e)
            trace_back_limit = None
        self.sds_context.exception_and_close(exception_str, trace_back_limit)

    def execute_with_lineage(self) -> Tuple[JavaObject, str]:
        """If not already created, create a preparedScript from our DMLCode, pass python local data to our prepared
        script, then execute our script and return the resultVariables

        :return: resultVariables of our execution and the string lineage trace
        """
        # we could use the gateway directly, non defined functions will be automatically
        # sent to the entry_point, but this is safer
        try:
            connection = self.__prepare_script()
            connection.setLineage(True)
            ret = self.prepared_script.executeScript()

            if len(self.out_var_name) == 1:
                return ret, self.prepared_script.getLineageTrace(self.out_var_name[0])
            else:
                traces = []
                for output in self.out_var_name:
                    traces.append(self.prepared_script.getLineageTrace(output))
                return ret, traces

        except Py4JNetworkError:
            exception_str = "Py4JNetworkError: no connection to JVM, most likely due to previous crash or closed JVM from calls to close()"
            trace_back_limit = 0
        except Exception as e:
            exception_str = str(e)
            trace_back_limit = None
        self.sds_context.exception_and_close(exception_str, trace_back_limit)

    def __prepare_script(self):
        gateway = self.sds_context.java_gateway
        entry_point = gateway.entry_point
        if self.prepared_script is None:
            input_names = self.inputs.keys()
            connection = entry_point.getConnection()
            self.prepared_script = connection.prepareScript(
                self.dml_script,
                _list_to_java_array(gateway, input_names),
                _list_to_java_array(gateway, self.out_var_name))
            for (name, input_node) in self.inputs.items():
                input_node.pass_python_data_to_prepared_script(
                    self.sds_context, name, self.prepared_script)
            return connection

    def get_lineage(self) -> str:
        gateway = self.sds_context.java_gateway
        entry_point = gateway.entry_point
        if self.prepared_script is None:
            input_names = self.inputs.keys()
            connection = entry_point.getConnection()
            self.prepared_script = connection.prepareScript(
                self.dml_script,
                _list_to_java_array(gateway, input_names),
                _list_to_java_array(gateway, self.out_var_name))
            for (name, input_node) in self.inputs.items():
                input_node.pass_python_data_to_prepared_script(
                    gateway.jvm, name, self.prepared_script)

            connection.setLineage(True)

        self.prepared_script.executeScript()
        if len(self.out_var_name) == 1:
            return self.prepared_script.getLineageTrace(self.out_var_name[0])
        else:
            traces = []
            for output in self.out_var_name:
                traces.append(self.prepared_script.getLineageTrace(output))
            return traces

    def build_code(self, dag_root: DAGNode) -> None:
        """Builds code from our DAG

        :param dag_root: the topmost operation of our DAG, result of operation will be output
        """
        baseOutVarString = self._dfs_dag_nodes(dag_root)
        if dag_root.output_type != OutputType.NONE:
            if dag_root.output_type == OutputType.MULTI_RETURN:
                self.out_var_name = []
                for idx, output_node in enumerate(dag_root._outputs):
                    self.add_code(
                        f'write({baseOutVarString}_{idx}, \'./tmp_{idx}\');')
                    self.out_var_name.append(f'{baseOutVarString}_{idx}')
            else:
                self.out_var_name.append(baseOutVarString)
                self.add_code(f'write({baseOutVarString}, \'./tmp\');')

    def clear(self, dag_root: DAGNode):
        self._dfs_clear_dag_nodes(dag_root)
        self._variable_counter = 0

    def _dfs_dag_nodes(self, dag_node: VALID_INPUT_TYPES) -> str:
        """Uses Depth-First-Search to create code from DAG

        :param dag_node: current DAG node
        :return: the variable name the current DAG node operation created
        """
        if not isinstance(dag_node, DAGNode):
            if isinstance(dag_node, bool):
                return 'TRUE' if dag_node else 'FALSE'
            return str(dag_node)

        # If the node already have a name then it is already defined
        # in the script, therefore reuse.
        if dag_node.dml_name != "":
            return dag_node.dml_name

        if dag_node._source_node is not None:
            self._dfs_dag_nodes(dag_node._source_node)
        # for each node do the dfs operation and save the variable names in `input_var_names`
        # get variable names of unnamed parameters

        unnamed_input_vars = []
        for un_node in dag_node.unnamed_input_nodes:
            unnamed_input_vars.append(self._dfs_dag_nodes(un_node))

        named_input_vars = {}
        for name, input_node in dag_node.named_input_nodes.items():
            named_input_vars[name] = self._dfs_dag_nodes(input_node)

        # check if the node gets a name after multireturns
        # If it has, great, return that name
        if dag_node.dml_name != "":
            return dag_node.dml_name

        dag_node.dml_name = self._next_unique_var()

        if dag_node.is_python_local_data:
            self.add_input_from_python(dag_node.dml_name, dag_node)

        code_line = dag_node.code_line(
            dag_node.dml_name, unnamed_input_vars, named_input_vars)

        self.add_code(code_line)
        return dag_node.dml_name

    def _dfs_clear_dag_nodes(self, dag_node: VALID_INPUT_TYPES) -> str:
        if not isinstance(dag_node, DAGNode):
            return
        dag_node.dml_name = ""
        for n in dag_node.unnamed_input_nodes:
            self._dfs_clear_dag_nodes(n)
        for name, n in dag_node.named_input_nodes.items():
            self._dfs_clear_dag_nodes(n)
        if dag_node._source_node is not None:
            self._dfs_clear_dag_nodes(dag_node._source_node)

    def _next_unique_var(self) -> str:
        """Gets the next unique variable name

        :return: the next variable name (id)
        """
        var_id = self._variable_counter
        self._variable_counter += 1
        return f'V{var_id}'

# Helper Functions
def _list_to_java_array(gateway: JavaGateway, py_list: Union[Collection[str], KeysView[str]]) -> JavaArray:
    """Convert python collection to java array.

    :param py_list: python collection
    :return: java array
    """
    array = gateway.new_array(gateway.jvm.java.lang.String, len(py_list))
    for (i, e) in enumerate(py_list):
        array[i] = e
    return array


import os, sys

sys.path.append(os.path.abspath(os.path.curdir))

from Tools import mainParserStepN, particleNumbers, col
options = mainParserStepN()
particleTags = particleNumbers()

if __name__ == '__main__':
    # List of energies to shoot
    energies = options.energies
    if energies is None or len(energies) == 0:
        print('Energies not specified. '
        'Using default values that might not work in your case.')
        energies = [1,3,5,10,15,20,25,30]

    # List of etas to shoot particles
    etaTags = options.eta
    if etaTags is None or len(etaTags) == 0:
        print('Etas not specified. '
        'Using default values that might not work in your case.')
        etaTags = ['1p7']
    etas = {}
    for etaTag in etaTags:
        etas[etaTag] = float(etaTag.replace("p","."))

    # List of phi to shoot particles
    phiTags = options.phi
    if phiTags is None or len(phiTags) == 0:
        print('%sWarning%s: Phi not specified. The script is not '
        'going to specify a Phi.'%(col.magenta,col.endc))
        phiTags = ['notSet']
    phis = {}
    for phiTag in phiTags:
        if phiTag != 'notSet':
            phis[phiTag] = float(phiTag.replace("p","."))

    # List of particles to generate in pdg codes
    particles = options.particles
    if particles is None or len(particles) == 0:
        print('%sWarning%s: Particles not specified. Using Gamma as default. '
        'This might not be compatible with your configuration.'%(col.magenta,col.endc))
        particles = [22]

    # Getting environment info
    cmssw = os.environ['CMSSW_VERSION']
    cmsswBase = os.environ['CMSSW_BASE']
    user = os.environ['USER']
    genDir = '%s/src/Configuration/GenProduction/python/'%cmsswBase
    cwd = os.getcwd()

    # Get filenames from previous step
    eTag = ''
    for E in energies:
        eTag = '%s %d'%(eTag,E)
    pTag = ''
    for p in particles:
        pTag = '%s %d'%(pTag,p)
    etaList = ''
    for etaTag in etaTags:
        etaList = '%s %s'%(etaList,etaTag)
    phiList = ''
    for phiTag in phiTags:
        if phiTag != 'notSet':
            phiList = '%s %s'%(phiList,phiTag)
    os.system("sh createList.sh step3 '%s' '%s' '%s' '%s' '%s' '%s' '%s' "
    "'%s' "%(eTag,pTag,options.geometry,etaList,phiList,options.tag,options.closeBy,options.campaign))
    filein = open('myGeneration/list.txt','r')

    for p in particles:
        for E in energies:
            for etaTag in etaTags:
                for phiTag in phiTags:
                    outTag = ''
                    if options.closeBy:
                        outTag = 'CloseBy'
                    particleTag = particleTags[p]
                    outTag = '%sSingle%s'%(outTag,particleTag)
                    outTag = '%s_E%d'%(outTag,E)
                    outTag = '%sEta%s'%(outTag,etaTag)
                    if phiTag != 'notSet':
                        outTag = '%sPhi%s'%(outTag,phiTag)
                    os.chdir(cwd)
                    os.system('cp ntuplesConfig.py myGeneration/%s/'%outTag)
                    os.chdir('myGeneration/%s'%outTag)

                    # Create CRAB configuration file
                    file1 = open('crabConfig_%s_ntuples.py'%outTag,'w')
                    file1.write('# Script automatically generated using ntuples.py\n\n')

                    file1.write('from CRABClient.UserUtilities ')
                    file1.write('import config\n')
                    file1.write('config = config()\n')
                    file1.write("config.General.requestName = ")
                    if options.campaign is None or options.campaign == None or options.campaign == 'None':
                        if options.tag is None or options.tag == None or options.tag == 'None':
                            file1.write("'%s_%s_upgrade2026_%s_ntuples'\n"%(outTag,cmssw,options.geometry))
                        else:
                            file1.write("'%s_%s_upgrade2026_%s_%s_ntuples'\n"%(outTag,cmssw,options.geometry,options.tag))
                    else:
                        if options.tag is None or options.tag == None or options.tag == 'None':
                            file1.write("'%s_%s_upgrade2026_%s_%s_ntuples'\n"%(outTag,cmssw,options.geometry,options.campaign))
                        else:
                            file1.write("'%s_%s_upgrade2026_%s_%s_%s_ntuples'\n"%(outTag,cmssw,options.geometry,options.campaign,options.tag))
                    file1.write("config.General.workArea = 'crab_projects'\n")
                    file1.write("config.General.transferOutputs = True\n")
                    file1.write("config.General.transferLogs = True\n\n")

                    file1.write("config.JobType.pluginName = 'Analysis'\n")
                    file1.write("config.JobType.psetName = ")
                    file1.write("'ntuplesConfig.py'\n")
                    file1.write("config.JobType.maxJobRuntimeMin = 50\n")
                    file1.write("config.JobType.maxMemoryMB = 5000\n\n")

                    file1.write("config.Data.inputDataset = '%s'\n"%((filein.readline())[:-1]))
                    file1.write("config.Data.inputDBS = 'phys03'\n")
                    file1.write("config.Data.splitting = 'FileBased'\n")
                    file1.write("config.Data.unitsPerJob = %d\n"%options.unitsPerJob)
                    file1.write("config.Data.totalUnits = %d\n"%options.njobs)
                    file1.write("config.Data.outLFNDirBase = '/store/user/%s/'\n"%user)
                    file1.write("config.Data.publication = False\n")
                    file1.write("config.Data.outputDatasetTag = ")
                    if options.campaign is None or options.campaign == None or options.campaign == 'None':
                        file1.write("'%s_%s_upgrade2026_%s_ntuples'\n\n"%(outTag,cmssw,options.geometry))
                    else:
                        file1.write("'%s_%s_upgrade2026_%s_%s_ntuples'\n\n"%(outTag,cmssw,options.geometry,options.campaign))

                    file1.write("config.Site.storageSite = 'T3_US_FNALLPC'\n")
                    file1.write("config.Site.blacklist = ['T2_US_Caltech']\n")
                    file1.close()

                    if options.no_exec:
                        os.system('crab submit -c crabConfig_%s_ntuples.py'%outTag)

os.chdir(cwd)
os.system('rm myGeneration/list.txt')


from unittest import TestCase

import torch

from ..embedding_evaluator import EmbeddingEvaluator

class EmbeddingEvaluatorTest(TestCase):

    def test_l2_distance(self):
        a = torch.rand([123, 234])
        b = a
        distance = EmbeddingEvaluator.l2_distance(a, b)
        self.assertTrue(torch.all(distance.diagonal() == 0))

    def test_cosine_distance(self):
        a = torch.rand([123, 234])
        b = a
        distance = EmbeddingEvaluator.cosine_distance(a, b)
        self.assertTrue(torch.all(distance.diagonal() < 1e-5))


# 给你两个有序整数数组 nums1 和 nums2，请你将 nums2 合并到 nums1 中，使 nums1 成为一个有序数组。
# https://leetcode-cn.com/problems/merge-sorted-array/

class Solution1:
    def merge(self, nums1, m, nums2, n) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        Python 列表内置方法
        """
        nums1[:] = sorted(nums1[:m] + nums2)

if __name__ == "__main__":
    nums1 = [1,2,3,0,0,0]
    m = 3
    nums2 = [2,5,6]
    n = 3

    s1 = Solution1()
    s1.merge(nums1, m, nums2, n)
    print(nums1)


from api import permissions as api_permissions

class HasEditPostPermission(api_permissions.HasPermissionMixin):
    permission_name = 'api.change_post'

class HasEditPostPermissionOrReadOnly(api_permissions.HasPermissionOrReadOnlyMixin):
    permission_name = 'api.change_post'

class HasViewPostPermission(api_permissions.HasViewPermissionIfPublishedMixin):
    permission_name = 'api.view_post'

    def has_object_permission(self, request, view, obj):
        if obj.show_on_main_page:
            return True

        return super(HasViewPostPermission, self).has_object_permission(request, view, obj)


#!/usr/bin/env python3

# Project Euler : Problem 10
# Summation of primes

from math import sqrt

primes = [2]
s = 2

i = 3
while primes[-1] < 2000000:
    isPrime = True
    for p in primes:
        if p > sqrt(i):
            break

        if i % p == 0:
            isPrime = False
            break

    if isPrime:
        primes.append(i)
        s += i

    i += 2

s -= primes[-1]

print(s)

# Solution : 142913828922

# Notes :
# Même technique que le problème 7


# -*- coding: utf-8 -*-
import logging
_logger = logging.getLogger(__name__)

from openerp import api, models, fields
from openerp.exceptions import Warning

class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    ar_qt_activity_type = fields.Selection(
        [
            ('todocesped', 'Todocesped'),
            ('arelux', 'Arelux'),
            ('evert', 'Evert'),
        ],
        size=15,
        string='Tipo de actividad',
        store=True
    )
    ar_qt_customer_type = fields.Selection(
        [
            ('particular', 'Particular'),
            ('profesional', 'Profesional'),
        ],
        string='Tipo de cliente',
        store=True
    )

    @api.model
    def _prepare_refund(self, invoice, date_invoice=None, date=None, description=None, journal_id=None):
        return_prepare_refund = super(AccountInvoice, self)._prepare_refund(invoice, date_invoice, date, description, journal_id)
        return_prepare_refund['ar_qt_activity_type'] = invoice.ar_qt_activity_type
        return_prepare_refund['ar_qt_customer_type'] = invoice.ar_qt_customer_type
        return return_prepare_refund

    @api.multi
    def action_invoice_open(self):
        return_account_invoice = super(AccountInvoice, self).action_invoice_open()

        if self.ar_qt_customer_type==False:
            self.ar_qt_customer_type = self.partner_id.ar_qt_customer_type

        if self.ar_qt_activity_type==False:
            if self.partner_id.ar_qt_activity_type=='both':
                self.ar_qt_activity_type = 'todocesped'
            else:
                self.ar_qt_activity_type = self.partner_id.ar_qt_activity_type

        if self.origin!=False:
            origins = self.origin.split(',')
            sale_order_ids = self.env['sale.order'].search([('name', '=', origins[0])])

            find_sale_order_ids = False
            if len(sale_order_ids)>0:
                for sale_order_id in sale_order_ids:
                    self.ar_qt_activity_type = sale_order_id.ar_qt_activity_type
                    self.ar_qt_customer_type = sale_order_id.ar_qt_customer_type

                    find_sale_order_ids = True

            if find_sale_order_ids==False:
                account_invoice_ids = self.env['account.invoice'].search([('number', '=', origins[0]),('type', '=', 'out_invoice')])
                if len(account_invoice_ids)>0:
                    for account_invoice_id in account_invoice_ids:
                        self.ar_qt_activity_type = account_invoice_id.ar_qt_activity_type
                        self.ar_qt_customer_type = account_invoice_id.ar_qt_customer_type

        return return_account_invoice


# Documentation: https://scikit-learn.org/stable/modules/neighbors.html#nearest-neighbor-algorithms

import meshio
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import Rbf

mesh = meshio.read("circle-2d-drag_1000.vtu")
#cells=mesh.cells_dict
pointData=mesh.point_data
Velocity=pointData['Velocity'][:,[0,1]] # extracting the x and y velocity
points=mesh.points
nodal_coordinates=points[:,[0,1]] # extracting x,y data only

#%% Cut out the portion of the data before the cylinder
# see computing_the_cutoff_index.py in the same directory

xlowerlim =  0.44088176352705416
xupperlim =  1.8737474949899802
ylowerlim =  0.06180904522613064
yupperlim =  0.3399497487437185

# boolean to data to be trimmed

bool_data=np.zeros((len(nodal_coordinates)),dtype=bool)
# make sure the data is within x limit
x_limit_bool=np.logical_and(nodal_coordinates[:,0]<xupperlim, nodal_coordinates[:,0]>xlowerlim)
# make sure the data is within y limit
y_limit_bool=np.logical_and(nodal_coordinates[:,1]<yupperlim, nodal_coordinates[:,1]>ylowerlim)
# Using logical and to make sure the data is within both the limits
trimmed_bool_array=np.logical_and(x_limit_bool,y_limit_bool)
nodal_coordinates=nodal_coordinates[trimmed_bool_array]
Velocity=Velocity[trimmed_bool_array]

# for i in range (0,len(nodal_coordinates)):
#     bool_data[i]= ((nodal_coordinates[i,0]<xupperlim and nodal_coordinates[i,0]>xlowerlim) and (nodal_coordinates[i,1]<yupperlim and nodal_coordinates[i,1]>ylowerlim))

# nodal_coordinates[(nodal_coordinates[:,0]<xupperlim & nodal_coordinates[:,0]>xlowerlim) & (nodal_coordinates[:,1]<yupperlim & nodal_coordinates[:,1]>ylowerlim)]

# temp2=nodal_coordinates[bool_data]

#%% Point from structured grid

#Creating structured grid on trimmed mesh

# # Boundaries of the structured grid
# xlowerlim=(min(nodal_coordinates=[:,0]))
# xupperlim=(max(nodal_coordinates=[:,0]))

# ylowerlim=(min(nodal_coordinates=[:,1]))
# yupperlim=(max(nodal_coordinates=[:,1]))

# Number of nodes in the grid
num_x=500
num_y=200

x=np.linspace(xlowerlim,xupperlim,num_x)
y=np.linspace(ylowerlim,yupperlim,num_y)

x_meshgrid,y_meshgrid=np.meshgrid(x,y)

#%%
# RBF interpolation and prediction
def RBF_interpolant(x,y,values):
    return Rbf(x,y,values) # all these arrays should have same size

def RBF_prediction(RBF_model,x_meshgrid,y_meshgrid):
    return RBF_model(x_meshgrid,y_meshgrid)

RBF_model=RBF_interpolant(nodal_coordinates[:,0],nodal_coordinates[:,1],Velocity[:,0])
u_x=RBF_prediction(RBF_model,x_meshgrid,y_meshgrid)

plt.figure(2)
plt.pcolor(x_meshgrid,y_meshgrid,u_x,cmap='viridis')#,vmin=0, vmax=4)
plt.gca().set_aspect('equal', adjustable='box')
plt.colorbar(orientation='horizontal')

# It works really well

#%% Sample data points
'''
Credit of this part of code goes to:
Rudy SH, Brunton SL, Proctor JL, Kutz JN. Data-driven discovery of partial differential equations. Science Advances. 2017 Apr 1;3(4):e1602614.
Now randomly sample some points to use.
'''
print('Picking up random points from the data')
# Sample a collection of data points, stay away from edges so I can use interpolation.
# The coordinates is chosen randomly from the sliced data. That is why random.choice is used here
np.random.seed(0) # generates same set of random numbers each time
num_xy = 5000 # number of x and y points to be taken randomly in the collection
num_t = 60  # number of timesteps to be taken in the collection
num_points = num_xy * num_t  # total number of points in the collection
# Trimming the data that involves the cylinder
boundary = 5 # Selected points in y axis [boundary:m-boundary]
boundary_x = 11 # Selected points in x axis [boundary_x:n-boundary_x]
points = {}  # dictionary to store the collection
count = 0

3. nearest neighbour in the vtu file/nearest_neightbour.py


import CONSOLE_MODULE
import CONSOLE_MODULE__POA
from Acspy.Servants.ContainerServices      import ContainerServices
from Acspy.Servants.ComponentLifecycle  import ComponentLifecycle
from Acspy.Servants.ACSComponent         import ACSComponent
import TYPES
import SYSTEMErr

MODE_MANUAL = False
MODE_AUTOMATIC = True

class ConsoleComponentImpl(CONSOLE_MODULE__POA.Console,
                             ACSComponent,
                             ContainerServices,
                             ComponentLifecycle):
    def __init__(self):
        ACSComponent.__init__(self)
        ContainerServices.__init__(self)
        self._logger = self.getLogger()
        self.mode = MODE_MANUAL
    #Override ComponentLifecycle methods

    def initialize(self):
         #try:
         #    lamp = self.getComponent("LAMP1")
         #    self.brightness = lamp._get_brightness()
         #except Exception, e:
         #    print "LAMP1 unavailable:", e
        pass

    def cleanUp(self):
        #self.releaseComponent("LAMP1")
        pass

     #Implementation of IDL methods
     #def sayHello(self):
     #    return "hello"
    def setMode(self, mode):
        # mode: True -> Automatic mode
        #       False -> Manual mode

        # Try to go to Manual mode
        #if not mode:
        if mode == MODE_MANUAL:
            # If current mode is Manual, do nothing
            #if not self.mode:
            if self.mode == MODE_MANUAL:
                pass
            # If current mode is Automatic, stop scheduler and change current mode
            else:
                scheduler = self.getComponent("SCHEDULER")
                scheduler.stop()
                self.mode = mode
        # Try to go to Automatic mode
        else:
            # If current mode is Manual, start scheduler and change current mode
            #if not self.mode:
            if self.mode == MODE_MANUAL:
                scheduler = self.getComponent("SCHEDULER")
                scheduler.start()
                self.mode = mode
            # If current mode is Automatic, raise SYSTEMErr::AlreadyInAutomaticEx exception
            else:
                raise SYSTEMErr.AlreadyInAutomaticEx

    def getMode(self):
        return self.mode

    def moveTelescope(self, position):
        if self.mode == MODE_AUTOMATIC:
            raise SYSTEMErr.SystemInAutoModeEx

        # Move the telescope to position.az, position.el
        # pos=TYPES.Position(45,55)
        # SYSTEMErr::SystemInAutoModeEx, SYSTEMErr::PositionOutOfLimitsEx)
        self._logger.logInfo("Moving the telescope to EL {} AZ {}".format(position.el, position.az))

        telescope = self.getComponent("TELESCOPE")
        try:
            telescope.moveTo(position)
        except SYSTEMErr.PositionOutOfLimitsEx as e:
            self._logger.logInfo("Requested position is out of limit: EL {} AZ {}".format(position.el, position.az))

    def getTelescopePosition(self):
        #return self.Position.az, self.Position.el
        telescope = self.getComponent("TELESCOPE")
        position = telescope.getCurrentPosition()
        self._logger.logInfo("Current position is: EL {} AZ {}".format(position.el, position.az))
        return position

    def cameraOn(self):
        if self.mode == MODE_AUTOMATIC:
            raise SYSTEMErr.SystemInAutoModeEx

        # Turn the camera on
        self._logger.logInfo("Turning camera on")
        instrument = self.getComponent("INSTRUMENT")
        instrument.cameraOn()

    def cameraOff(self):
        if self.mode == MODE_AUTOMATIC:
            raise SYSTEMErr.SystemInAutoModeEx

        # Turn the camera off
        self._logger.logInfo("Turning camera off")
        instrument = self.getComponent("INSTRUMENT")
        instrument.cameraOff()

    def getCameraImage(self):
        if self.mode == MODE_AUTOMATIC:
            raise SYSTEMErr.SystemInAutoModeEx

        # Get image from camera
        self._logger.logInfo("Getting image from instrument")
        instrument = self.getComponent("INSTRUMENT")

        # Error when passing parameter 1 to takeImage?
        image = instrument.takeImage(1)
        return image

        #img = [2,3,12,55,255]
        #octseq = str(bytearray(image))
        #return octseq

    def setRGB(self, rgbConfig):
        # Set RGB configuration in camera
        self._logger.logInfo("Setting RGB configuration in instrument:{}, {}, {}".format(rgbConfig.red, rgbConfig.green, rgbConfig.blue))
        instrument = self.getComponent("INSTRUMENT")
        instrument.setRGB(rgbConfig)

    def setPixelBias(self, bias):
        # Set pixel bias configuration in camera
        self._logger.logInfo("Setting pixel bias in instrument: {}".format(bias))
        instrument = self.getComponent("INSTRUMENT")
        instrument.setPixelBias(bias)

    def setResetLevel(self, resetLevel):
        # Set reset level configuration in instrument
        self._logger.logInfo("Setting reset level in instrument: {}".format(resetLevel))
        instrument = self.getComponent("INSTRUMENT")
        instrument.setResetLevel(resetLevel)

    def sayHelloWithParameters(self, inString, inoutDouble):
        self.getLogger().logInfo("sayHello called with arguments inString="
                                 + inString
                                 + "; inoutDouble="
                                 + str(inoutDouble)
                                 + ". Will return 'hello'..."
                                                  )
        return ("hello", inoutDouble, 23)

#Main defined only for generic testing
if __name__ == "__main__":
    g = Console()


# ****************************************************************
# Author: Sheldon Dorgelo
# Date (last edited): 03/11/2020
# Purpose: checks the list of known orbitting object and compares
#          them to detected candidates. If candidate is close to
#          position of a known orbitting object, that orbitting object
#          is added to the list of objects to plot
# ****************************************************************

import DataObject
import ephem
import math
import multiprocessing
from astropy.wcs import WCS #used for calculating ra and dec of
import matplotlib.pyplot as plt #used for creating a new plot
from matplotlib.patches import Polygon

def orbits(dataObject):
    gatech = ephem.Observer()

    more = dataObject.getCandGt() # get the detected candidates
    less = dataObject.getCandLt()
    tle = dataObject.getTleData() # get the TLE or 3LE file
    tlel = dataObject.getTleLength() # get length of that file
    elementLines = 2 # for TLE files (default value)
    wcs = dataObject.getWcsInfo()

    # contains the lists for ra and dec of detected candidates
    candidateRA = []
    candidateDec = []

    manager = multiprocessing.Manager()
    matchesRA1 = manager.list()
    matchesRA2 = manager.list()
    matchesRA3 = manager.list()
    matchesRA4 = manager.list()
    matchesDec1 = manager.list()
    matchesDec2 = manager.list()
    matchesDec3 = manager.list()
    matchesDec4 = manager.list()
    firstMatchRA1 = manager.list()
    firstMatchRA2 = manager.list()
    firstMatchRA3 = manager.list()
    firstMatchRA4 = manager.list()
    firstMatchDec1 = manager.list()
    firstMatchDec2 = manager.list()
    firstMatchDec3 = manager.list()
    firstMatchDec4 = manager.list()
    matchesName1 = manager.list()

    tm = dataObject.getImageHeader()['DATE'] # extracts the date/time from the header of the image
    time = tm[:4]+'/'+tm[5:7]+'/'+tm[8:10]+' '+tm[11:13]+':'+tm[14:16]+':'+tm[17:] # formatted time
    date = ephem.Date(time)

    if(tle[0].startswith("0")): #check if the number of lines per element is 3
        elementLines = 3 # for 3LE files

    # calculate ra and dec for bright candidate objects and add to above lists
    for i in range(len(more[0])):
        r,d = wcs.all_pix2world(more[1][i], more[0][i],0)
        candidateRA.append(r)
        candidateDec.append(d)

    # calculate ra and dec for dark candidate objects and add to above lists
    for j in range(len(less[0])):
        r,d = wcs.all_pix2world(less[1][j], less[0][j],0)
        candidateRA.append(r)
        candidateDec.append(d)

    # set up multiprocessing to reduce computational times
    processA = multiprocessing.Process(target = calculatePositions,
        args = [-60, -30, gatech, date, tle, tlel, elementLines, candidateRA,
        candidateDec, matchesRA1, matchesDec1, matchesName1, firstMatchRA1, firstMatchDec1])
    processB = multiprocessing.Process(target = calculatePositions,
        args = [-30, 0, gatech, date, tle, tlel, elementLines, candidateRA,
        candidateDec, matchesRA2, matchesDec2, matchesName1, firstMatchRA2, firstMatchDec2])
    processC = multiprocessing.Process(target = calculatePositions,
        args = [0, 30, gatech, date, tle, tlel, elementLines, candidateRA,
        candidateDec, matchesRA3, matchesDec3, matchesName1, firstMatchRA3, firstMatchDec3])
    processD = multiprocessing.Process(target = calculatePositions,
        args = [30, 60, gatech, date, tle, tlel, elementLines, candidateRA,
        candidateDec, matchesRA4, matchesDec4, matchesName1, firstMatchRA4, firstMatchDec4])

    # do processing
    processA.start()
    processB.start()
    processC.start()
    processD.start()
    #wait until processes are finished
    processA.join()
    processB.join()
    processC.join()
    processD.join()

    #combine the lists and convert them into a python list
    matchesRA = list(matchesRA1) + list(matchesRA2) + list(matchesRA3) + list(matchesRA4)
    matchesDec = list(matchesDec1) + list(matchesDec2) + list(matchesDec3) + list(matchesDec4)
    firstMatchesRA = list(firstMatchRA1) + list(firstMatchRA2) + list(firstMatchRA3) + list(firstMatchRA4)
    firstMatchesDec = list(firstMatchDec1) + list(firstMatchDec2) + list(firstMatchDec3) + list(firstMatchDec4)
    matchesName = list(matchesName1)

    image = dataObject.getImageData()
    imageData = [] # will contain 4 elements: {[RA,dec(BL)], [RA,dec(BR)], [RA,dec(TR)], [RA,dec(TL)]}
    #INCREASING y coordinate correlates to INCREASING RA
    #INCREASING x coordinate correlates to DECREASING Dec

    # calculate ra and dec of all corners in the image
    imageData.append(wcs.all_pix2world(len(image[0]),0,0)) #bottom left
    imageData.append(wcs.all_pix2world(len(image[0]),len(image),0)) #bottom right
    imageData.append(wcs.all_pix2world(0,len(image),0)) # top right
    imageData.append(wcs.all_pix2world(0,0,0)) #top left

    # create and save the plot to data object
    dataObject.setOrbitPlot(createFigure(matchesRA, matchesDec, candidateRA, candidateDec, imageData,
        firstMatchesRA, firstMatchesDec, matchesName))

def calculatePositions(start, end, gatech, date, tle, tlel, elementLines,
    candidateRA, candidateDec, matchesRA, matchesDec, matchesName, firstMatchRA, firstMatchDec):
    for dt in range(start, end): # total time = 120 seconds
        gatech.date = date + dt/86400 # 86400 = number of seconds in a day
        for i in range(0, tlel-1, elementLines): # for each element in catalog
            try:          #"junk" is a placeholder string
                # FOR TLE: "junk", i, i+1; FOR 3LE: "junk", i+1, i+2
                sat = ephem.readtle("junk",tle[i+elementLines-2],tle[i+elementLines-1])
                sat.compute(gatech)
                ra = math.degrees(float(repr(ephem.degrees(sat.ra))))
                dec = math.degrees(float(repr(ephem.degrees(sat.dec))))

                for j in range(len(candidateRA)): # compare against each detected candidate
                    if(abs(candidateRA[j]-ra) < 2 and abs(candidateDec[j]-dec) < 2):
                        matchesRA.append(ra)
                        matchesDec.append(dec)
                        #add name of orbitting object to list to be annotated in plot later (only for 3LE files)
                        if(elementLines == 3):
                            if not tle[i][2:] in matchesName: # get the actual name instead of "junk" if its a 3LE file
                                matchesName.append(tle[i][2:]) #name to be annotated without the '0 ' in front
                                firstMatchRA.append(ra)    #coordinates for annotation
                                firstMatchDec.append(dec)
            except RuntimeError as e:
                print(e) #probably a problem with the tle/3le file

def createFigure(matchesRA, matchesDec, candidateRA, candidateDec, imageData,
                 firstMatchesRA, firstMatchesDec, matchesName):
    newFigure = plt.figure(figsize=(12,8))

    plt.scatter(matchesRA, matchesDec, c = 'g') # plot orbitting objects in green
    plt.scatter(candidateRA, candidateDec, c = 'r') # plot candidate objects in red

    # set limits on x and y
    lowRA = min(candidateRA) - 2
    highRA = max(candidateRA) + 2
    lowDec = min(candidateDec) - 2
    highDec = max(candidateDec) + 2
    plt.xlim(lowRA, highRA)
    plt.ylim(lowDec, highDec)
    plt.xlabel("RA (deg)")
    plt.ylabel("Dec (deg)")

    #place names as annotations for orbiting objects
    for i in range(len(matchesName)):
        plt.annotate(matchesName[i],(firstMatchesRA[i], firstMatchesDec[i]),
            color='black',fontsize='small',ha='center')

    #add border to display the edges of the actual image
    plt.gca().add_patch(Polygon(imageData,fill=False,color='black',linewidth=1))

    return newFigure


from django import template
from django.db.models import Count

from ..models import Category

register = template.Library()

@register.simple_tag
def get_all_categories(project_queryset):
    queryset = Category.objects.annotate(
        count_projects=Count('projects')
    ).filter(projects__in=project_queryset)
    return queryset


import pandas as pd

import matplotlib.pyplot as plt

act = ['WatchEvent', 'IssueCommentEvent','IssuesEvent', 'MemberEvent','PullRequestEvent','ForkEvent']
cont = ['CreateEvent', 'DeleteEvent', 'PushEvent', 'CommitCommentEvent', 'ReleaseEvent','PublicEvent', 'PullRequestReviewCommentEvent', 'GollumEvent']
sm = []
sact = []
scont = []

for i in range(1, 10):
    df = pd.read_csv('data/2018_0' + str(i) + '.csv')
    sm.append(sum([sum(df[col]) for col in df.columns.values]))
    sact.append(sum([sum(df[col]) for col in act]))
    scont.append(sum([sum(df[col]) for col in cont]) - 2 * sum(df['DeleteEvent']))

for i in range(10, 13):
    df = pd.read_csv('data/2018_' + str(i) + '.csv')
    sm.append(sum([sum(df[col]) for col in df.columns.values]))
    sact.append(sum([sum(df[col]) for col in act]))
    scont.append(sum([sum(df[col]) for col in cont]) - 2 * sum(df['DeleteEvent']))

print(sm)
print(sact)
print(scont)

month = list(range(1, 13))

plt.figure(figsize=(12,8))
l1=plt.plot(month,sm,'r--',label='number of events')
l2=plt.plot(month,sact,'g--',label='activities')
l3=plt.plot(month,scont,'b--',label='contributions')

plt.plot(month,sm,'ro-',month,sact,'g+-',month,scont,'b^-')
plt.title('Events in different months')
plt.xlabel('month')
plt.ylabel('value')

plt.legend()
plt.show()


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /Users/svpino/dev/tensorflow-object-detection-sagemaker/todl/tensorflow-object-detection/research/street/python/vgsl_input.py
# Compiled at: 2020-04-05 19:50:58
# Size of source mod 2**32: 6733 bytes
"""String network description language to define network layouts."""
import collections, tensorflow as tf
from tensorflow.python.ops import parsing_ops
ImageShape = collections.namedtuple('ImageTensorDims', [
 'batch_size', 'height', 'width', 'depth'])

def ImageInput(input_pattern, num_threads, shape, using_ctc, reader=None):
    """Creates an input image tensor from the input_pattern filenames.

  TODO(rays) Expand for 2-d labels, 0-d labels, and logistic targets.
  Args:
    input_pattern:  Filenames of the dataset(s) to read.
    num_threads:    Number of preprocessing threads.
    shape:          ImageShape with the desired shape of the input.
    using_ctc:      Take the unpadded_class labels instead of padded.
    reader:         Function that returns an actual reader to read Examples from
      input files. If None, uses tf.TFRecordReader().
  Returns:
    images:   Float Tensor containing the input image scaled to [-1.28, 1.27].
    heights:  Tensor int64 containing the heights of the images.
    widths:   Tensor int64 containing the widths of the images.
    labels:   Serialized SparseTensor containing the int64 labels.
    sparse_labels:   Serialized SparseTensor containing the int64 labels.
    truths:   Tensor string of the utf8 truth texts.
  Raises:
    ValueError: if the optimizer type is unrecognized.
  """
    data_files = tf.gfile.Glob(input_pattern)
    assert data_files, 'no files found for dataset ' + input_pattern
    queue_capacity = shape.batch_size * num_threads * 2
    filename_queue = tf.train.string_input_producer(data_files,
      capacity=queue_capacity)
    images_and_label_lists = []
    for _ in range(num_threads):
        image, height, width, labels, text = _ReadExamples(filename_queue, shape, using_ctc, reader)
        images_and_label_lists.append([image, height, width, labels, text])

    images, heights, widths, labels, truths = tf.train.batch_join(images_and_label_lists,
      batch_size=(shape.batch_size),
      capacity=(16 * shape.batch_size),
      dynamic_pad=True)
    labels = tf.deserialize_many_sparse(labels, tf.int64)
    sparse_labels = tf.cast(labels, tf.int32)
    labels = tf.sparse_tensor_to_dense(labels)
    labels = tf.reshape(labels, [shape.batch_size, -1], name='Labels')
    heights = tf.reshape(heights, [-1], name='Heights')
    widths = tf.reshape(widths, [-1], name='Widths')
    truths = tf.reshape(truths, [-1], name='Truths')
    images = tf.identity(images, name='Images')
    tf.summary.image('Images', images)
    return (images, heights, widths, labels, sparse_labels, truths)

def _ReadExamples(filename_queue, shape, using_ctc, reader=None):
    """Builds network input tensor ops for TF Example.

  Args:
    filename_queue: Queue of filenames, from tf.train.string_input_producer
    shape:          ImageShape with the desired shape of the input.
    using_ctc:      Take the unpadded_class labels instead of padded.
    reader:         Function that returns an actual reader to read Examples from
      input files. If None, uses tf.TFRecordReader().
  Returns:
    image:   Float Tensor containing the input image scaled to [-1.28, 1.27].
    height:  Tensor int64 containing the height of the image.
    width:   Tensor int64 containing the width of the image.
    labels:  Serialized SparseTensor containing the int64 labels.
    text:    Tensor string of the utf8 truth text.
  """
    if reader:
        reader = reader()
    else:
        reader = tf.TFRecordReader()
    _, example_serialized = reader.read(filename_queue)
    example_serialized = tf.reshape(example_serialized, shape=[])
    features = tf.parse_single_example(example_serialized, {'image/encoded':parsing_ops.FixedLenFeature([
      1],
       dtype=tf.string, default_value=''),
     'image/text':parsing_ops.FixedLenFeature([
      1],
       dtype=tf.string, default_value=''),
     'image/class':parsing_ops.VarLenFeature(dtype=tf.int64),
     'image/unpadded_class':parsing_ops.VarLenFeature(dtype=tf.int64),
     'image/height':parsing_ops.FixedLenFeature([
      1],
       dtype=tf.int64, default_value=1),
     'image/width':parsing_ops.FixedLenFeature([
      1],
       dtype=tf.int64, default_value=1)})
    if using_ctc:
        labels = features['image/unpadded_class']
    else:
        labels = features['image/class']
    labels = tf.serialize_sparse(labels)
    image = tf.reshape((features['image/encoded']), shape=[], name='encoded')
    image = _ImageProcessing(image, shape)
    height = tf.reshape(features['image/height'], [-1])
    width = tf.reshape(features['image/width'], [-1])
    text = tf.reshape((features['image/text']), shape=[])
    return (
     image, height, width, labels, text)

def _ImageProcessing(image_buffer, shape):
    """Convert a PNG string into an input tensor.

  We allow for fixed and variable sizes.
  Does fixed conversion to floats in the range [-1.28, 1.27].
  Args:
    image_buffer: Tensor containing a PNG encoded image.
    shape:          ImageShape with the desired shape of the input.
  Returns:
    image:        Decoded, normalized image in the range [-1.28, 1.27].
  """
    image = tf.image.decode_png(image_buffer, channels=(shape.depth))
    image.set_shape([shape.height, shape.width, shape.depth])
    image = tf.cast(image, tf.float32)
    image = tf.subtract(image, 128.0)
    image = tf.multiply(image, 0.01)
    return image


from solr_query_test import get_citances_for_file
from collections import defaultdict
from bs4 import BeautifulSoup
from tqdm import tqdm

def write_new_files(lines, queries_to_remove, fn, fn_skipped):
    new_lines = []
    skipped_lines = []
    for line in lines:
        query = line.split("\t")[0]
        if query in queries_to_remove:
            skipped_lines.append(line)
        else:
            new_lines.append(line)

    with open(fn, "w") as f:
        f.write("".join(new_lines))

    with open(fn_skipped, "w") as f:
        f.write("".join(skipped_lines))

if __name__ == "__main__":
    # Relevant files to overwrite
    with open("satya_input_search_only.tsv") as f:
        lines_2_search = f.readlines()
    with open("satya_input_search_only_3.tsv") as f:
        lines_3_search = f.readlines()
    with open("satya_input_with_truth.tsv") as f:
        lines_2_truth = f.readlines()
    with open("satya_input_with_truth_3.tsv") as f:
        lines_3_truth = f.readlines()

    # This is the validation set used by the UoM team (winner 2019)
    valid = sorted(["C00-2123", "C04-1089", "I05-5011", "J96-3004", "N06-2049", "P05-1004", "P05-1053", "P98-1046"])

    folder = "./data/Training-Set-2019/Task1/From-Training-Set-2018/"
    queries_to_remove = []
    for filename in tqdm(valid):  # Used for the validation runs with the UoM data
        citances = get_citances_for_file(filename, list(), folder)

        for i, citance in enumerate(citances, start=1):
            all_query_sentences = citance["Citation Text"]
            soup = BeautifulSoup(all_query_sentences, "html.parser")
            texts = soup.findAll()
            satya_input_query = ""
            # Add text together from different citation sentences.
            # Results show that a big single query performs better.
            for el in texts:
                satya_input_query += el.text
                # print(cleaned+"\n")
            queries_to_remove.append(satya_input_query)

    print(len(queries_to_remove))
    write_new_files(lines_2_search, queries_to_remove, "satya_input_search_only.tsv", "2_search_test.tsv")
    write_new_files(lines_3_search, queries_to_remove, "satya_input_search_only_3.tsv", "3_search_test.tsv")
    write_new_files(lines_2_truth, queries_to_remove, "satya_input_with_truth.tsv", "2_truth_test.tsv")
    write_new_files(lines_3_truth, queries_to_remove, "satya_input_with_truth_3.tsv", "3_truth_test.tsv")


import sys
sys.stdin = open("선택정렬.txt","r")
T = int(input())
def select(list, k): # 가장큰값 가장작은값 번갈아가며 뽑는것.
    for i in range(0,k) :
        if i%2 == 0:
            maxindex = i
            for j in range(i, len(list)):
                if list[maxindex] <= list[j]:
                    maxindex = j

            list[i], list[maxindex] = list[maxindex], list[i]
        if i%2 != 0:
            minindex = i
            for j in range(i, len(list)):
                if list[minindex] >= list[j]:
                    minindex = j
            list[i], list[minindex] = list[minindex], list[i]

    return list

for tcn in range(T):

    lenth = int(input())
    a = list(map(int, input().split()))

    ans = select(a, lenth)
    ans2 = ans[0:10]

    print('#{}'.format(tcn+1), end=" ")
    for k in ans2:
        print(k, end=" ")
    print()


#!/usr/bin/env python
# coding: utf-8
'''
This script reads in a phenix.refine mtz file.
It plots the R-factor by resolution bin.
The plots are made with matplotlib using miller arrays.
It also plots the correlation coefficients.
The plots were made with matplotlib.

This script was adapted from an example script in iotbx:

Source:  https://github.com/cctbx/cctbx_project/blob/master/
iotbx/examples/recalculate_phenix_refine_r_factors.py
'''

# get_ipython().run_line_magic("matplotlib", "inline")

from __future__ import absolute_import, division, print_function
from iotbx.reflection_file_utils import get_r_free_flags_scores
from iotbx.file_reader import any_file
import matplotlib
import matplotlib.pyplot as plt

def compute_r_factors(fobs, fmodel, flags):
  fmodel, fobs = fmodel.common_sets(other=fobs)
  fmodel, flags = fmodel.common_sets(other=flags)
  fc_work = fmodel.select(~(flags.data()))
  fo_work = fobs.select(~(flags.data()))
  fc_test = fmodel.select(flags.data())
  fo_test = fobs.select(flags.data())
  r_work = fo_work.r1_factor(fc_work)
  r_free = fo_test.r1_factor(fc_test)

  print("r_work = %.4f" % r_work)
  print("r_free = %.4f" % r_free)
  print("")

  binner = flags.setup_binner(n_bins=20)
  d_star_power = 1.618034
  centers = binner.bin_centers(d_star_power)
  d_centers = list(centers**(-1 / d_star_power))
#   for i in d_centers:
#     print(i)

  fo_work.use_binning_of(flags)
  fc_work.use_binner_of(fo_work)
  fo_test.use_binning_of(fo_work)
  fc_test.use_binning_of(fo_work)

  r_work_list = []
  r_free_list = []
  cc_work_list = []
  cc_free_list = []
  for i_bin in fo_work.binner().range_all():
    sel_work = fo_work.binner().selection(i_bin)
    sel_test = fo_test.binner().selection(i_bin)
    fo_work_bin = fo_work.select(sel_work)
    fc_work_bin = fc_work.select(sel_work)
    fo_test_bin = fo_test.select(sel_test)
    fc_test_bin = fc_test.select(sel_test)
    if fc_test_bin.size() == 0 : continue

    r_work_bin = fo_work_bin.r1_factor(other=fc_work_bin,
      assume_index_matching=True)
    r_work_list.append(r_work_bin)

    r_free_bin = fo_test_bin.r1_factor(other=fc_test_bin,
      assume_index_matching=True)
    r_free_list.append(r_free_bin)

    cc_work_bin = fo_work_bin.correlation(fc_work_bin).coefficient()
    cc_work_list.append(cc_work_bin)

    cc_free_bin = fo_test_bin.correlation(fc_test_bin).coefficient()
    cc_free_list.append(cc_free_bin)

    legend = flags.binner().bin_legend(i_bin, show_counts=False)
    print("%s  %8d %8d  %.4f %.4f  %.3f %.3f" % (legend, fo_work_bin.size(),
      fo_test_bin.size(), r_work_bin, r_free_bin, cc_work_bin, cc_free_bin))

  return d_centers, r_work_list, r_free_list, cc_work_list, cc_free_list

def plot_r_factors(d_centers, r_work_list, r_free_list):
  plt.scatter(d_centers, r_work_list, label=r"$\mathit{R_{work$")
  plt.scatter(d_centers, r_free_list, label=r"$\mathit{R_{free$")
  plt.xlabel(r"Resolution ($\mathrm{\AA$)")
  plt.ylabel(r"R-factor (%)")
  plt.legend(loc="upper right")
  plt.savefig("Rs.pdf")
  plt.close()

def plot_cc(d_centers, cc_work_list, cc_free_list):
  plt.scatter(d_centers, cc_work_list, label=r"$\mathit{CC_{work$")
  plt.scatter(d_centers, cc_free_list, label=r"$\mathit{CC_{free$")
  plt.xlabel(r"Resolution ($\mathrm{\AA$)")
  plt.ylabel(r"Correlation Coefficeint Fo vs Fc (%)")
  plt.legend(loc="lower right")
  plt.savefig("CCs.pdf")

def run(input_mtz):
  mtz_in = any_file(input_mtz)
  ma = mtz_in.file_server.miller_arrays
  flags = fmodel = fobs = None
  # select the output arrays from phenix.refine.  This could easily be modified
  # to handle MTZ files from other programs.
  for array in ma :
    labels = array.info().label_string()
    if labels.startswith("R-free-flags"):
      flags = array
    elif labels.startswith("F-model"):
      fmodel = abs(array)
    elif labels.startswith("F-obs-filtered"):
      fobs = array
  if (None in [flags, fobs, fmodel]):
    raise RuntimeError("Not a valid phenix.refine output file")
  scores = get_r_free_flags_scores([flags], None)
  test_flag_value = scores.test_flag_values[0]
  flags = flags.customized_copy(data=flags.data()==test_flag_value)

  (d_centers,
   r_work_list,
   r_free_list,
   cc_work_list,
   cc_free_list) = compute_r_factors(fobs, fmodel, flags)
  plot_r_factors(d_centers, r_work_list, r_free_list)
  plot_cc(d_centers, cc_work_list, cc_free_list)

if (__name__ == "__main__"):
  run(input_mtz="28molrepEdited_5_refine_001.mtz")