import getopt
import sys
from os import listdir
from os.path import join
import os
import random
import torch
from torch.utils import data
import glob
import numpy as np
import scipy.misc
import cv2
import warnings
warnings.filterwarnings("ignore", category=UserWarning)

class DRIVEData(data.Dataset):
    '''
    Finish the implementation of our custom dataloader class.
    We will use the test images as our validation set.
    '''

    def __init__(self,
                 root,
                 split='train',
                 patch_size=64,
                 scale_factor=255
                 ):
        self.ROOT_PATH = root
        self.split = split
        self.patch_size = patch_size
        self.scale_factor = scale_factor
        self.TRAIN_PATH = os.path.join(root, 'training')
        self.TEST_PATH = os.path.join(root, 'testing')
        if split == 'train':
            self.data_folder = self.TRAIN_PATH
        elif split == 'test':
            self.data_folder = self.TEST_PATH
        else:
            raise ValueError
        self.sample_names = listdir(self.data_folder)

    def __len__(self):
        return len(self.sample_names)

    def select_patch(self, pixels_img, pixels_seg, size, width, height):
        # select coordinates for patch
        x = random.randint(0, width - size)
        y = random.randint(0, height - size)

        return pixels_img[x:x+size, y:y+size, :], pixels_seg[x:x+size, y:y+size]

    def concat_patch(self, pixels_img, pixels_seg, size=256):
        # size = patch_size
        # break down the image into patches of desired size and stack the patches
        # for example, image of (512,512,3) => (4, 256, 256, 3)
        factor = int(512/size)
        img_concat = np.zeros((factor**2, size, size, 3))
        seg_concat = np.zeros((factor**2, size, size))
        for i in range(0, factor):
            for j in range(0, factor):
                img_concat[i*factor+j, :, :, :] = pixels_img[size *
                                                             i:size*(i+1), size*j:size*(j+1), :]
                seg_concat[i*factor+j, :, :] = pixels_seg[size *
                                                          i:size*(i+1), size*j:size*(j+1)]
        return img_concat, seg_concat

    def __getitem__(self, i):  # i is index
        assert self.split in ['train', 'test', 'result']

        sample_name = self.sample_names[i]
        full_img_G_path = join(self.data_folder, sample_name, 'HE-green.png')
        full_img_B_path = join(self.data_folder, sample_name, 'HE-blue.png')
        full_img_R_path = join(self.data_folder, sample_name, 'HE-red.png')
        full_nec_path = join(self.data_folder, sample_name, 'necrosis.png')
        full_perf_path = join(self.data_folder, sample_name, 'perfusion.png')
        full_label_path = join(self.data_folder, sample_name, 'EF5.png')

        im_img_G = cv2.imread(full_img_G_path, cv2.IMREAD_GRAYSCALE)
        im_img_B = cv2.imread(full_img_B_path, cv2.IMREAD_GRAYSCALE)
        im_img_R = cv2.imread(full_img_R_path, cv2.IMREAD_GRAYSCALE)
        im_nec = cv2.imread(full_nec_path, cv2.IMREAD_GRAYSCALE)
        im_perf = cv2.imread(full_perf_path, cv2.IMREAD_GRAYSCALE)
        im_label = cv2.imread(full_label_path, cv2.IMREAD_GRAYSCALE)
        assert im_img_G.ndim == 2
        assert isinstance(im_img_G, np.ndarray)
        assert im_img_G.shape == im_label.shape
        width, height = im_img_G.shape

        if self.split == 'train' or self.split == 'test':
            # normalize to [0,1]
            pixels_img_G = np.array(im_img_G)/255
            pixels_img_B = np.array(im_img_B)/255
            pixels_img_R = np.array(im_img_R)/255
            pixels_nec = np.array(im_nec)/255
            pixels_perf = np.array(im_perf)/255
            pixels_seg = np.array(im_label)/self.scale_factor
            # make multiple chanels
            pixels_img = np.stack(
                [pixels_img_G, pixels_img_B, pixels_img_R, pixels_nec, pixels_perf], axis=2)
            # make sure there is something in the label img
            assert pixels_seg.max() > 60/self.scale_factor
            patch_img, patch_seg = self.select_patch(
                pixels_img, pixels_seg, self.patch_size, width, height)
            # do not patch the background
            # while patch_img.mean() < (7.6 / 255):
            #     # print('do not patch the background\n')
            #     patch_img, patch_seg = self.select_patch(
            #         pixels_img, pixels_seg, self.patch_size, width, height)

            img = torch.tensor(patch_img).permute(2, 0, 1)
            seg = torch.tensor(patch_seg).unsqueeze(0)
        else:
            pass
            # patch_img = im_img.resize((512,512)) #resample = NEAREST
            # patch_seg = im_seg.resize((512,512))
            # pixels_img = np.array(patch_img)/255
            # pixels_seg = np.array(patch_seg)/255
            # # for eval reconstruction
            # # make sure the input patch_size to concat_patch() equals args.patch_size
            # # which corresponds to the patch size model was trained on
            # patch_img, patch_seg = self.concat_patch(pixels_img, pixels_seg, 256)
            # img = torch.tensor(patch_img).permute(0,3,1,2)
            # seg = torch.tensor(patch_seg).unsqueeze(1)

        '''
        I have done the pipelining, your task is to:

        1. Load the imgs to numpy arrays.
            Hint: Use the PIL.Image library for this.
            Note: img should have shape 3xHxW
                  seg should have shape HxW. Furthermore,
                  each pixel in seg should have a value indexing
                  which class it is apart of. For this dataset,
                  we are segmenting the blood vessels from the
                  background class. Thus, there are only two classes,
                  0 = background class, 1 = blood vessel class.

        2. Normalize the images to [0, 1] or z-score norm.
            Hint: if you z-score normalize, you should consider
            whether to normalize each RGB channel seperately or
            normalize all channels simultaneously.

        3. Rescale the images to target size.
            Note: target size is your choice, but
            it should be appropriate depending on your
            model of choice. I.e, some models require
            every dimension to be divisible by 16 due
            to pooling layers. Also the choice of interpolation
            matters.
            Hint: seg only has only viable interpolation method
            (since the values are discrete, {0, 1}) while img has a few options.

        4. Apply augmentations here.

        5. Turn the numpy arrays into torch tensors.
        '''

        data_dict = {
            "name": sample_name,
            "image": img,
            "mask": seg,
            # "class": diagnosis
        }

        return data_dict

if __name__ == '__main__':
    dataset = DRIVEData(
        root='./data/hypoxia img')


from unified.core.app import App
from emails.gmail.actions import GmailActions
from emails.gmail.apis import GmailApi
from emails.gmail.triggers import GmailTriggers

class GmailApp(App, GmailActions, GmailApi, GmailTriggers):

    def __init__(self):
        super().__init__(
            name="Gmail",
            description="Gmail is a free email service provided by Google.",
            category="Emails",
            logo="https://logo.500apps.com/Gmail",
            auth_info=None,
            auth_type='oauth2'
        )


'''导入类'''

# 导入多个类
from Chapter9_Class import Car, ElectricCar
# from Chapter9_Class import ElectricCar

my_new_car = Car('audi', 'a4', 2016)
print(my_new_car.get_descriptive_name())

my_new_car.odometer_reading = 23
my_new_car.read_odometer()

my_tesla = ElectricCar('tesla', 'model s', 2016)

print(my_tesla.describe_battery())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

#导入整个模块
import Chapter9_Class
my_beetle = Chapter9_Class.Car('volkswagen', 'beetle', 2016)

#导入模块中的所有类 不推荐这种方式 使用是用module_name.class_name语法访问类
from Chapter9_Class import *

#python标准库
from collections import OrderedDict

favorite_languages = OrderedDict()

favorite_languages['jen'] = 'python'
favorite_languages['sarah'] = 'c'
favorite_languages['edward'] = 'ruby'
favorite_languages['phil'] = 'python'

for name, language in favorite_languages.items():
    print(name.title() + "'s favorite language is " + language.title() + ".")


import requests
import yaml

with open('config.yaml', encoding='utf-8') as f:
    data = yaml.safe_load(f)

S = requests.Session()

def test_step1(user_login, post_title):
    result = S.get(url=data['address'], headers={'X-Auth-Token': user_login}, params={'owner': 'notMe'}).json()['data']
    result_title = [i['title'] for i in result]
    assert post_title in result_title, 'test_step1 FAIL'

# Новый тест для создания и проверки нового поста
def test_create_and_check_new_post(user_login, new_post_data):
    # Создаем новый пост
    response = S.post(
        url=data['address'],
        headers={'X-Auth-Token': user_login},
        data=new_post_data
    )
    assert response.status_code == 200, 'Failed to create a new post'

    # Получаем описание нового поста
    new_post_description = new_post_data['description']

    # Проверяем, есть ли созданный пост на сервере по полю "описание"
    result = S.get(url=data['address'], headers={'X-Auth-Token': user_login}).json()['data']
    result_descriptions = [i['description'] for i in result]
    assert new_post_description in result_descriptions, 'New post not found'


from typing import List, Tuple

# Approach 1 - Simpler
def separateEvenOdd(inputList: List) -> Tuple:
    even = []
    odd = []
    for element in inputList:
        if element % 2 == 0:
            even.append(element)
        else:
            odd.append(element)
    return even, odd

# Approach 2 - List Comprehensions
def separateEvenOddComprehension(inputList: List) -> Tuple:
    even = [x for x in inputList if x % 2 == 0]
    odd = [x for x in inputList if x % 2 != 0]
    return even, odd

even1, odd1 = separateEvenOdd(inputList=[10, 41, 30, 15, 80])
print(even1)
print(odd1)

even2, odd2 = separateEvenOddComprehension(inputList=[10, 41, 30, 15, 80])
print(even2)
print(odd2)


import cv2
import numpy as np

#load 2 images
img1 = cv2.imread('egimg1.png')
img2 = cv2.imread('egimg2.png')
#mergebothimages
#add = img1 + img2
#add = cv2.add(img1,img2)#addedallpixelvaluestogether
weighted = cv2.addWeighted(img1, 0.6, img2, 0,4,0)#imposing them on one another identically

cv2.imshow('weighted',weighted)
cv2.waitKey(0)
cv2.destroyAllWindows()


def solution(arr, queries):
    answer = []
    for q in queries:
        array = []
        for i in range(q[0], q[1]+1):
            if arr[i] > q[2]:
                array.append(arr[i])
        if not len(array):
            answer.append(-1)
        else:
            answer.append(min(array))
    return answer

수열과 구간 쿼리 2.py


from _datetime import datetime
import json

def decorator_logger(function):
    def get_info(*args, **kwargs):
        date = datetime.today().replace(microsecond=0)
        new_function = function(*args, **kwargs)
        information = f'Дата и время {date}, функция {decorator_logger.__name__}, аргументы: {args}, {kwargs}'
        with open('logger.json', 'w', encoding='utf-8') as f:   # запись в файл
            json.dump(information, f, ensure_ascii=False, indent=2)
        print(f'Данные записаны в файл', 'logger.json')
        return new_function
    return get_info

@decorator_logger
def logger(first_name, last_name):
    return first_name, last_name

logger('Иван', 'Иванов')


def read_graph():
	N, M = [int(x) for x in input().split()]
	graph = [(None, []) for i in range(N)]
	for j in range(M):
		a, b = [int(x) for x in input().split()]
		graph[a][1].append(b)
		graph[b][1].append(a)
	return graph, N, M

def skat(that, graph, used, path, k = 0):
	graph[that] = k % 2, graph[that][1]
	if graph[that][0] == 0:
		path.append(that)
	used.add(that)
	for other in graph[that][1]:
		if other in used and graph[that][0] == graph[other][0]:
			print('NO')
			exit(0)
		if other not in used:
			path = skat(other, graph, used, path, k + 1)
	return path

graph, n, m = read_graph()
path = []
used = set()
for i in range(n):
	if i not in used:
		path = skat(i, graph, used, path)
print(' '.join(map(str, path)))


from stockstats import StockDataFrame
from Models.Position import Position
import pandas, sys

class RSI_OverSold:

	def __init__(self, queuesHandler, dataProvider):
		self.queuesHandler = queuesHandler
		self.dataProvider = dataProvider
		self.__ignore_warnings()

	def perform(
    	self,
    	ticker,
    	rsi_period = 5,
    	rsi_limit = 20,
    	tp_percentage = 0.5,
    	amount = None,
    	ltf_interval="5m",
    	ltf_period="2d",
    	days_between_orders=2,
    	max_positions_amount=3,
    ):
		#print(locals())
		print(ticker)

		if amount is None:
			amount = self.queuesHandler.get_default_amount()
			print("Default Amount is: {}".format(amount))

		if self.queuesHandler.is_there_a_recent_order(ticker, days_between_orders):
			print('There is a recent order for {}'.format(ticker))
			return

		positions_amount = int(self.queuesHandler.get_positions_amount(ticker))
		print('There are {} opened positions for {}'.format(positions_amount, ticker))

		if max_positions_amount <= positions_amount:
			print('Already reached the limit of positions for {}'.format(ticker))
			return

		# Calculate RSI using Lower TimeFrame (ltf) data
		data_ltf = self.dataProvider.get(symbol = ticker, interval = ltf_interval, period = ltf_period)

		data_ltf = self.__fill_data_gaps(data_ltf)

		df_ltf = StockDataFrame.retype(data_ltf)

		rsi_key = 'rsi_' + str(rsi_period)

		rsi_serie = df_ltf[rsi_key]

		current_rsi = round(rsi_serie[-1], 2)
		previous_rsi = round(rsi_serie[-2], 2)

		#self.__print_df_tail(df_ltf)
		print("Current RSI({}): {}.. Previous RSI value: {}".format(rsi_period, current_rsi, previous_rsi))

		if previous_rsi < rsi_limit:
			takeProfit = amount * tp_percentage / 100

			self.queuesHandler.add_position_to_open(Position(
				ticker = ticker,
				amount = amount,
				stopLoss = False,
				takeProfit = round(takeProfit, 2),
			))

			self.queuesHandler.save_order(ticker)

			print('Opening a position for {}. Take profit {}'.format(ticker, takeProfit))

	def __crossover(self, series, limit, offset = 1):
		last_index = - offset
		previous_index = last_index - 1

		last_value = round(series[last_index], 2)
		previous_value = round(series[previous_index], 2)

		print("Last: {}, Previous: {}".format(last_value, previous_value))

		if( previous_value <= limit and last_value > limit):
			return True
		return False

	def __ignore_warnings(self):
		import warnings, logging
		warnings.filterwarnings("ignore")
		logging.disable(logging.CRITICAL)

	def __print_df_tail(self, df, rows=5):
		pandas.set_option('display.max_columns', None)
		print(df.tail(rows))
		sys.stdout.flush()

	def __print_full(self, df):
		import pandas as pd
		pandas.set_option('display.max_rows', len(df))
		pandas.set_option('display.max_columns', None)
		print(df)
		pandas.reset_option('display.max_rows')
		sys.stdout.flush()

	def __fill_data_gaps(self, df, resample = '5min'):

		# Fill data gaps
		df = df.resample(resample).ffill()

		return df.between_time('09:30', '16:00')


################################################################################
# Factorization algorithms
################################################################################
from math import floor, ceil
from gmpy2 import mpz,mpq,mpfr,mpc
from gmpy2 import is_square, isqrt, sqrt, log2, gcd, is_prime, next_prime, primorial
from .factordb_connector import *
from time import time
import sympy as sp
import os

MSIEVE_BIN = os.environ.get("MSIEVE_BIN", "/tmp/ggnfs-bin/")
YAFU_BIN   = os.environ.get("YAFU_BIN",   "NONE")
CADO_BIN   = os.environ.get("CADO_BIN",   "NONE")
YAFU_THREADS   = os.environ.get("YAFU_THREADS",   "4")
YAFU_LATHREADS = os.environ.get("YAFU_LATHREADS", "4")

#Factoring libraries
USE_PARI = True if ( (YAFU_BIN == "NONE") or (YAFU_BIN == "NONE")) else False

if USE_PARI:
  import cypari2 as cp
  cyp = cp.Pari()
  cyp.default("parisizemax", 1<<29 )
  cyp.default("threadsizemax", 1<<27 )
  pari_cfactor = cyp.factorint

# This gives direct access to the integer factoring engine called by most arithmetical functions. flag is optional; its binary digits mean
# 1: avoid MPQS,
# 2: skip first stage ECM (we may still fall back to it later),
# 4: avoid Rho and SQUFOF,
# 8: don’t run final ECM (as a result, a huge composite may be declared to be prime).
#    Note that a (strong) probabilistic primality test is used, thus composites might not be detected, although no example is known.

#Flags
SKIP_MPQS          = 1
SKIP_ECM_STAGE_ONE = 2
SKIP_RO_SQUFOF     = 4
SKIP_ECM_STAGE_TWO = 8

def pollard(n, limit=1000):
    x = 2
    y = 2
    d = 1
    l = 0
    def g(x):
      return x*x + 1
    while d == 1 and l <= limit:
        x = g(x)
        y = g(g(y))
        d = gcd(abs(x - y), n)
        #print(n,l)
        l += 1
    if n > d > 1:
      return n//d, d
    else:
      return []

def cadonfs_factor_driver(n):
  global CADO_BIN
  print("[*] Factoring %d with yafu..." % n)
  tmp = []
  proc = subprocess.Popen([ CADO_BIN, str(n)], stdout=subprocess.PIPE)
  for line in proc.stdout:
    line = line.rstrip().decode("utf8")
    if re.search("\d+",line):
      tmp += [int(x) for x in line.split(" ")]
  return tmp

def msieve_factor_driver(n):
  global MSIEVE_BIN
  print("[*] Factoring %d with msieve..." % n)
  import subprocess, re, os
  tmp = []
  proc = subprocess.Popen([MSIEVE_BIN,"-s","/tmp/%d.dat" % n,"-t","8","-v",str(n)],stdout=subprocess.PIPE)
  for line in proc.stdout:
    line = line.rstrip().decode("utf8")
    if re.search("factor: ",line):
      tmp += [int(line.split()[2])]
  os.system("rm %d.dat" % n)
  return tmp

def yafu_factor_driver(n, timeout = 60*30):
  global YAFU_BIN
  print("[*] Factoring %d with yafu..." % n)
  import subprocess, re, os
  tmp = []
  try:
     parse = subprocess.run([YAFU_BIN,"-one","-plan", "custom","-pretest_ratio","0.32", "-threads",YAFU_THREADS,"-lathreads",YAFU_LATHREADS, "-ggnfs_dir", MSIEVE_BIN, "-xover", "160", "-snfs_xover", "150",  str(n)], timeout=timeout, stdout=subprocess.PIPE)
  except subprocess.TimeoutExpired:
     print("Timeout: ", timeout, " Seconds. Moving on to next candidate")
     return [ "C" + str( n.bit_length() ) + " = " + str(n) ]

  parse = [ line for line in parse.stdout.decode('utf-8').split("\n") if "=" in line ]
  tmp = []
  flag=False

  for line in parse:
      if "Total factoring" in line:
          flag = True
          continue
      if "ans" in line:
          continue

      if flag:
          tmp.append(line)

  return tmp

def cfactor(n):
  print("[*] pari factoring: %d..." % n)
  f0 = pari_cfactor(n)
  factors  = [int(a) for a in f0[0]]
  return factors

def external_factorization(n,timeout=60*30):
  factors = []

  if USE_PARI:
      factors = cfactor(n)
  else:
      factors = yafu_factor_driver(n,timeout)

  #YAFU already uses msieve
  #if len(factors) == 0:
  #  factors = msieve_factor_driver(n)
  return factors

def factorization_handler(n,timeout=30*60):
  print("[*] Factoring:",n)

  #Turn this off for now. There seems to be an issue connecting to the database site.
  #F = getfdb(n)
  F = []

  if len(F) == 0:
    factors = external_factorization(n,timeout)
    print("[+] factors: %s" % str(factors))
  else:
    print("fdb got: %d factors: %s" % (len(F),str(F)))
    factors = []
    for f in F:
      if is_prime(f):
        factors += [f]
      else:
        factors += external_factorization(f)
  factors = sorted(factors)
  #There seems to be an issue with the website for FactorDB. Turn off for now.
  #send2fdb(n,factors)
  return factors

def ranged_primorial(L, p = 2):
  #print("pre sieving...",end="")
  tmp = 2
  c = 0
  while c < L:
    p = next_prime(p)
    tmp *= p
    c += 1
  #print("ok")
  return tmp, p

def prime_levels_load0(s,e):
  siever = [ 0, 1, 6, 5005 ]
  prev, last_p = ranged_primorial(8)
  for level in range(s,e):
    siever.append(ranged_primorial(1 << level, p = last_p))
    print("Level %d ok" % level)
  return siever

def prime_levels_load1(s,e):
  siever = [ 0, 1, 6, 5005 ]
  prev = sp.primorial( 1<<3, False)
  for level in range(s,e):
    current       = sp.primorial( 1 << level, False)
    level_n_sieve = current//prev
    prev          = current
    siever.append(  level_n_sieve )
    print("Level %d ok" % level)
  return siever

def prime_levels_load2(s,e):
  siever = [ 0, 1, 6, 5005 ]
  prev = primorial( 1<<3)
  for level in range(s,e):
    start = time()
    current       = primorial( 1 << level)
    level_n_sieve = current//prev
    prev          = current
    siever.append(  level_n_sieve )
    print("Level: %d primes: %d  ok Time: %f" % (level,(1 << level), time()-start ))
  return siever

def prime_levels_load_timing():
  t0 = time.time()
  prime_levels_load0(4,16)
  t1 = time.time()
  print(t1-t0)
  prime_levels_load1(4,16)
  t2 = time.time()
  print(t2-t1)
  prime_levels_load2(4,16)
  t3 = time.time()
  print(t3-t2)

prime_levels_load = prime_levels_load2

def SDC(W, candidates):
  print("[*] square difference check...")
  if is_square(W):
    a = isqrt(W)
    for n in candidates:
      n2 = abs(n - W)
      b = isqrt(n2)
      if (b * b) == n2:
        if n > gcd(a + b, n) > 1:
          factors = []
          print("[+] Factored using square difference...")
          if is_prime(a + b):
            factors += [a + b]
          if is_prime(a - b):
            factors += [a - b]
          if len(factors) == 2:
            return factors, n
  return None


class Contents(object):
    def __init__(self, size, files):
        self.size = size
        self.files = files

    def get_size(self):
        return self.size

    def get_files(self):
        return self.files

    def add_dir_to_size(self, size, dir=None):
        self.size = self.size + int(size)
        if dir is not None:
            self.files.remove(dir)

    def add_dir(self, dir):
        self.files.append(dir)

def day_7():
    input = open("input_day7.txt", "r").read()
    lines = input.split("\n")
    directories = {}
    full_path = ""
    directories["/"] = Contents(0, [])
    for line in lines:
        words = line.split(" ")
        if words[0] == "$":
            if words[1] == "cd" and words[2] == "..":
                full_path = full_path.rsplit("/", 2)[0] + "/"
            elif words[1] == "cd" and words[2] != "/":
                full_path = full_path + words[2] + "/"
                directories[full_path] = Contents(0, [])
            elif words[1] == "cd":
                full_path = "/"
        elif words[0] == 'dir':
            directories[full_path].add_dir(full_path + words[1] + "/")
        else:
            directories[full_path].add_dir_to_size(words[0])
    converged = False
    while not converged:
        converged = True
        for item in directories:
            children = directories[item].get_files()
            if children != []:
                for child in children:
                    grand = directories[child].get_files()
                    if grand == []:
                        directories[item].add_dir_to_size(directories[child].get_size(), child)
                    else:
                        converged = False
    return directories

def day_7a():
    directories = day_7()
    total = 0
    for item in directories:
        direct, size, children = item, directories[item].get_size(), directories[item].get_files()
        if size <= 100000:
            total = total + size
    return total

def day_7b():
    directories = day_7()
    space_available = 70000000 - directories["/"].get_size()
    space_needed = 30000000 - space_available
    smallest = 70000000
    for item in directories:
        direct, size, children = item, directories[item].get_size(), directories[item].get_files()
        if size >=  space_needed and size < smallest:
            smallest = size
    return smallest

print(day_7a())
print(day_7b())


# coding:utf-8
import tensorflow as tf
import sys, time
import numpy as np
import pickle
import Config
import T_Model
from utils_t import *

config_tf = tf.ConfigProto()
config_tf.gpu_options.allow_growth = True
config_tf.inter_op_parallelism_threads = 1
config_tf.intra_op_parallelism_threads = 1

config = Config.Config()
holiday = config.holiday
size = config.size
city = size
grids = str(50)
hidden = str(config.hidden_size)
idim = str(config.input_dim)
length = '10'
train_file = "../useroadnet/generate_data/data/train/train-"+size+"-grid"+grids+"-"+length+".txt"
print(train_file)
test_file = "../useroadnet/generate_data/data/test/test-"+size+"-grid"+grids+"-"+length+".txt"
roadinfo_file = "../useroadnet/generate_data/data/roadnet/road-"+size+"-"+grids+".txt"
embedding_file = '../useroadnet/generate_data/data/roadnet/road-'+ size + '-' + grids + '-' + idim + '-new.embedding'

# train_file = "../data/train/train-big-grid50.txt"
# test_file = "../data/test/test-big-ngrid50.txt"
# roadinfo_file = "../data/roadnet/road-big-50.txt"
# embedding_file = '../data/roadnet/road-'+ size + '-' + grids + '-' + idim + '.embedding'

# trainfile = open(train_file, 'r')
# roadinfo = open(roadinfo_file, 'r')
train_data = []
test_data = []
train_time = []
test_time = []
train_mean = 0
train_var = 0
test_mean = 0
test_var = 0
train_data0, embedding, features, weekday, data_size, _vocab_size, _ = load_data_grid(train_file, roadinfo_file, embedding_file, city)
print('tetjoiewjroiejrioewjri')
test_data0, __, __1, weekday1, data_size1, _vocab_size, _ = load_data_grid(test_file, roadinfo_file, embedding_file, city)
[grid2cor, type, tunnel, bridge, oneway] = features
for traId in train_data0:
    train_data.append(train_data0[traId])
    train_time.append(weekday[traId])
for traId in test_data0:
    test_data.append(test_data0[traId])
    test_time.append(weekday1[traId])

tmp_train = np.array(train_time)
tmp_train.reshape([-1])
tmp_test = np.array(test_time)
tmp_test.reshape([-1])
train_mean = np.mean(tmp_train)
train_var = np.var(tmp_train)
test_mean = np.mean(tmp_test)
test_var = np.var(tmp_test)
train_time = ((np.array(train_time)-train_mean)/train_var).tolist()
test_time = ((np.array(test_time)-train_mean)/train_var).tolist()
print(train_mean, train_var)

print('train data has %d characters, %d unique.' % (data_size, _vocab_size))
print(' test data has %d characters, %d unique.' % (data_size1, _vocab_size))
# char_to_idx = { ch:i for i, ch in enumerate(chars)}
# idx_to_char = { i:ch for i, ch in enumerate(chars)}

config.vocab_size = _vocab_size

types = config.add_dim
def data_iterator(raw_data, time_data, batch_size, num_steps):
    # print(raw_data[0][0][0][:int(idim)])
    if types!=0:
        raw_data0 = [[raw_data[i][j][0][:int(idim)]+raw_data[i][j][1]+raw_data[i][j][2]+raw_data[i][j][3]+raw_data[i][j][4] for j in range(len(raw_data[0]))] for i in range(len(raw_data))]
    else:
        raw_data0 = [[raw_data[i][j][0][:int(idim)] for j in range(len(raw_data[0]))] for i in range(len(raw_data))]
    raw_data1 = [[raw_data[i][j][0][int(idim)] for j in range(len(raw_data[0]))] for i in range(len(raw_data))]
    raw_data0 = np.array(raw_data0)
    raw_data1 = np.array(raw_data1)
    time_data = np.array(time_data)
    #    raw_data = np.array(raw_data, dtype=np.int32)
    # print(raw_data0.shape, raw_data1.shape)
    # print(type(raw_data), type(raw_data[0]))
    data_len = len(raw_data)
    batch_len = data_len // batch_size
    # data = np.zeros([batch_size, batch_len], dtype=np.int32)
    union_data = []
    valid_data = []
    for i in range(int(batch_len*0.9)):
        tmp = raw_data0[i * batch_size:(i + 1) * batch_size]
        tmp1 = raw_data1[i * batch_size:(i + 1) * batch_size]
        tmp_time = time_data[i * batch_size:(i + 1) * batch_size]
        # print(type(tmp[:, :num_steps, 0][:int(idim)]), type(tmp[:, :num_steps, 1]))
        x = tmp[:, :num_steps, :]
        y = tmp1[:, 1: num_steps+1]  # y就是x的错一位，即下一个词
        t = tmp_time
        y = np.reshape(y, (y.shape[0], -1))
        # print(x.shape, y.shape)
        union_data.append([x, y, t])
    for i in range(int(batch_len*0.9), batch_len):
        tmp = raw_data0[i * batch_size:(i + 1) * batch_size]
        tmp1 = raw_data1[i * batch_size:(i + 1) * batch_size]
        tmp_time = time_data[i * batch_size:(i + 1) * batch_size]
        # print(type(tmp[:, :num_steps, 0][:int(idim)]), type(tmp[:, :num_steps, 1]))
        x = tmp[:, :num_steps, :]
        y = tmp1[:, 1: num_steps + 1]  # y就是x的错一位，即下一个词
        t = tmp_time
        y = np.reshape(y, (y.shape[0], -1))
        # print(x.shape, y.shape)
        valid_data.append([x, y, t])
    return union_data, valid_data

def run_epoch(session, m, f_data, v_data, t_data, eval_op):
    """Runs the model on the given data."""
    epoch_size = len(f_data)
    print(epoch_size)
    start_time = time.time()
    costs = 0.0
    iters = 1

    # for i in range(len(state)):
    # print(len(state[i]))
    for step, temp in enumerate(f_data):
        x = temp[0]
        y = temp[1]
        t = temp[2]

        state = session.run(m.initial_state)

        cost, state, _ = session.run([m.cost, m.final_state, eval_op],  # x和y的shape都是(batch_size, num_steps)
                                     {m.input_data: x,
                                      m.targets: y,
                                      m.extra_data: t,
                                      m.initial_state: state})

        costs += cost
        iters += m.num_steps

        if step and step % (epoch_size // 10) == 0:
            print("%.2f perplexity: %.3f cost-time: %.2f s" %
                  (step * 1.0 / epoch_size, np.exp(costs / iters),
                   (time.time() - start_time)))
            start_time = time.time()

    costs1 = 0.0
    iters1 = 1
    for step, (x, y, t) in enumerate(v_data):

        state = session.run(m.initial_state)

        cost, state, _ = session.run([m.cost, m.final_state, tf.no_op()],  # x和y的shape都是(batch_size, num_steps)
                                     {m.input_data: x,
                                      m.targets: y,
                                      m.extra_data: t,
                                      m.initial_state: state})
        # cost, state, _ = session.run([m.cost, m.final_state, eval_op],  # x和y的shape都是(batch_size, num_steps)
        #                              feed_dict)
        costs1 += cost
        iters1 += m.num_steps
    costs2 = 0.0
    iters2 = 1
    for step, (x, y, t) in enumerate(t_data):
        # print(len(x[0]))
        # print(t.shape)
        # m.reset_state(m.batch_size)
        state = session.run(m.initial_state)
        # feed_dict = {m.input_data: x, m.targets: y, m.input_time: t}
        # for i in range(len(m.initial_state)):
        #     feed_dict[m.initial_state[i][0]] = m.initial_state[i][0].eval()
        #     feed_dict[m.initial_state[i][1]] = m.initial_state[i][1].eval()
        cost, state, _ = session.run([m.cost, m.final_state, tf.no_op()],  # x和y的shape都是(batch_size, num_steps)
                                     {m.input_data: x,
                                      m.targets: y,
                                      m.extra_data: t,
                                      m.initial_state: state})
        # cost, state, _ = session.run([m.cost, m.final_state, eval_op],  # x和y的shape都是(batch_size, num_steps)
        #                              feed_dict)
        costs2 += cost
        iters2 += m.num_steps
    return np.exp(costs / iters), np.exp(costs1 / iters1), np.exp(costs2/iters2)

def main(_):
    # train_data = context_of_idx

    with tf.Graph().as_default(), tf.Session(config=config_tf) as session:
        initializer = tf.random_uniform_initializer(-config.init_scale,
                                                    config.init_scale)
        with tf.variable_scope("model", reuse=False, initializer=initializer):
            m = T_Model.T_Model(is_training=True, holiday=holiday, config=config)

        tf.global_variables_initializer().run()

        model_saver = tf.train.Saver(tf.global_variables())
        index = 1
        train_data1, valid_data = data_iterator(train_data, train_time, m.batch_size, m.num_steps)
        # f_data = [x_f, y_f, t_f]
        test_data1, valid_data1 = data_iterator(test_data, test_time, m.batch_size, m.num_steps)
        # t_data = [x_t, y_t, t_t]
        for i in range(config.iteration):
            # if index%5 == 0:
            #     config.learning_rate = config.learning_rate * 0.95
            # m.lr_decay(0.1)
            # print(session.run(m.lr))
            print("Training Epoch: %d ..." % (i + 1))
            train_perplexity, valid_perplexity, test_perplexity = run_epoch(session, m, train_data1, valid_data, test_data1, m.train_op)
            print("Epoch: %d Train Perplexity: %.3f Valid Perplexity: %.3f Test Perplexity: %.3f" % (i + 1, train_perplexity, valid_perplexity, test_perplexity))

            if (i + 1) % config.save_freq == 0:
                print('model saving ...')
                model_saver.save(session, config.model_path+'-'+hidden+'-'+str(config.input_dim+types)+'-'+size +'-tgrid'+grids+'-'+holiday+'-%d' % (i + 1))
                print('Done!')

if __name__ == "__main__":
    # tf.app.run()
    main(1)


#!/usr/bin/env python

# NAME: Marcus Pearce, Ryan Lam
# EMAIL: marcuspearce7@g.ucla.edu, ryan.lam53@gmail.com
# ID: 305115848, 705124474
# CS 111 Project 3a

import sys
import csv

# DATA STRUCTURES

blockToInodeDict = {}                  # map block number -> list  [inode that reference it, indirection level, offset]

inodes = []
blocks = []
freeInodeList = set()           # all free inodes
freeBlockList = set()           # all free blocks
allocatedInodeList = set()      # all allocated inodes
dirents = []
inodeLinkCount = {}             # dictionary inode number -> link count

inodeToParentDict = {}            # maps inodeNum -> parent inodeNum

sb = None                       # superblock

class Superblock:
    def __init__(self, data):
        self.numBlocks = int(data[1])
        self.numInodes = int(data[2])
        self.inodeTableSize = int(data[2])*int(data[4]) / int(data[3])

class Inode:
    def __init__(self, data, free):
        self.num = int(data[1])
        self.free = free
        self.fileType = data[2]
        self.blockPointers = {} # map block number to logical offset
        self.linksCount = data[6]

class Block:
    def __init__(self, num):
        self.num = num

class Group:
    def __init__(self, data, inodeTableSize):
        self.dataBlockStart = int(data[8])+inodeTableSize

class Dirent:
    def __init__(self, data):
        self.parentInodeNum = int(data[1])
        self.inode = int(data[3])
        self.name = data[6]

# inode name inode_num

def main():
    # check for only one argument
    if(len(sys.argv) != 2):
        print("Incorrect arguments")
        exit(2)

    with open(sys.argv[1], "r") as f:
        lineReader = csv.reader(f)
        for row in lineReader:
            if row[0] == 'SUPERBLOCK': # get superblock data
                sb = Superblock(row)

            elif row[0] == 'GROUP': # get group data
                gt = Group(row, sb.inodeTableSize)
                for i in range(gt.dataBlockStart, sb.numBlocks): # init block dictionary
                    blockToInodeDict.update({i: []})

            elif row[0] == 'BFREE': # record free blocks
                blocknum = int(row[1])
                freeBlockList.add(blocknum)

            elif row[0] == 'IFREE': # record free inodes
                inodenum = int(row[1])
                freeInodeList.add(inodenum)

            elif row[0] == 'INODE': # record allocated inodes
                ti = Inode(row, 0)
                allocatedInodeList.add(ti.num)

                # block consistency audit -- check invalid and reserved blocks
                if(ti.fileType == 'f' or ti.fileType == 'd' or (ti.fileType == 's' and int(row[10]) > 60)):
                    for i in range(12, 27): # loop thru 15 block refs per inode
                        curblockref = int(row[i])
                        indirectlvl = 0
                        if(not curblockref == 0):
                            if(i == 24):
                                indirectlvl = 1
                                offset = 12
                                indirectstring = "INDIRECT "
                            elif(i == 25):
                                indirectlvl = 2
                                offset = 268
                                indirectstring = "DOUBLE INDIRECT "
                            elif(i == 26):
                                indirectlvl = 3
                                offset = 268 + (256*256)
                                indirectstring = "TRIPLE INDIRECT "
                            else:
                                offset = i - 12
                                indirectstring = ""

                            if(curblockref < 0 or curblockref > 63): # check invalid/reserved
                                print("INVALID " + indirectstring + "BLOCK " + str(curblockref) + " IN INODE " + row[1]
                                + " AT OFFSET " + str(offset))
                            elif(curblockref < gt.dataBlockStart):
                                print("RESERVED " + indirectstring + "BLOCK " + str(curblockref) + " IN INODE " + row[1]
                                + " AT OFFSET " + str(offset))
                            else:
                                ti.blockPointers.update({curblockref: offset}) # update inode object's block refs
                                blockToInodeDict[curblockref].append([int(row[1]), indirectlvl, offset])  # add inode reference to block dict

                inodes.append(ti) # add inode object to inodes list

            elif row[0] == 'INDIRECT': # record indirect block refs
                curblockref = int(row[5])
                indirectlvl = int(row[2])
                offset = 0
                if(indirectlvl == 1):
                    indirectstring = "INDIRECT"
                    offset = 12
                if(indirectlvl == 2):
                    indirectstring = "DOUBLE INDIRECT"
                    offset = 268
                if(indirectlvl == 3):
                    indirectstring = "TRIPLE INDIRECT"
                    offset = 268 + 256*256

                if(curblockref < 0 or curblockref > 63): # check invalid/reserved
                    print("INVALID " + indirectstring + " BLOCK " + str(curblockref) + " IN INODE " + row[1]
                    + " AT OFFSET " + row[3])
                elif(curblockref < gt.dataBlockStart):
                    print("RESERVED " + indirectstring + " BLOCK " + str(curblockref) + " IN INODE " + row[1]
                    + " AT OFFSET " + row[3])
                else:
                    blockToInodeDict[curblockref].append([int(row[1]), indirectlvl, offset])  # add inode reference to block dict

            elif row[0] == 'DIRENT':
                dirents.append(Dirent(row))

    f.close()

    # BLOCK consistency audit

    for x in blockToInodeDict:
        if (not len(blockToInodeDict[x]) == 0) and (x in freeBlockList):
            print("ALLOCATED BLOCK " + str(x) + " ON FREELIST")
        elif (len(blockToInodeDict[x]) == 0) and (not x in freeBlockList):
            print("UNREFERENCED BLOCK " + str(x))

    # process duplicate blocks
    for blockNum in blockToInodeDict:
        if len(blockToInodeDict[blockNum]) > 1:         # if more than one thing in list -> duplicate
            for info in blockToInodeDict[blockNum]:
                inodeNum = info[0]
                indirectlvl = info[1]
                offset = info[2]
                indirectstring = ""

                if indirectlvl == 1:
                    indirectstring = "INDIRECT "
                elif indirectlvl == 2:
                    indirectstring = "DOUBLE INDIRECT "
                elif indirectlvl == 3:
                    indirectstring = "TRIPLE INDIRECT "
                print("DUPLICATE " + indirectstring + "BLOCK " + str(blockNum) + " IN INODE " + str(inodeNum) + " AT OFFSET " + str(offset))

    # INODE allocation audit

    if(2 in freeInodeList) and (2 in allocatedInodeList):
            print("ALLOCATED INODE " + str(2) + " ON FREELIST")
    elif(not 2 in freeInodeList) and (not 2 in allocatedInodeList):
        print("UNALLOCATED INODE " + str(2) + " NOT ON FREELIST")

    for i in range(11, sb.numInodes+1):
        if(i in freeInodeList) and (i in allocatedInodeList):
            print("ALLOCATED INODE " + str(i) + " ON FREELIST")
        elif(not i in freeInodeList) and (not i in allocatedInodeList):
            print("UNALLOCATED INODE " + str(i) + " NOT ON FREELIST")

    # DIRECTORY consistency audits

    # zero out dictionaries to init
    for i in range(sb.numInodes):
        inodeLinkCount[i+1] = 0
    for i in range(len(dirents)):
        inodeToParentDict[i] = 0

    # scan all of directories to ennumerate all links, finding invalid and unallocated inodes
    for dirent in dirents:
        if dirent.inode > sb.numInodes or dirent.inode < 1:
            print("DIRECTORY INODE " + str(dirent.parentInodeNum) + " NAME " + dirent.name + " INVALID INODE " + str(dirent.inode))
        elif dirent.inode not in allocatedInodeList:
            print("DIRECTORY INODE " + str(dirent.parentInodeNum) + " NAME " + dirent.name + " UNALLOCATED INODE " + str(dirent.inode))
        else:
            inodeLinkCount[dirent.inode] += 1

        # for ".." case, find parent of each inode
        if dirent.name != "'.'" and dirent.name != "'..'":
            inodeToParentDict[dirent.inode] = dirent.parentInodeNum

    inodeToParentDict[2] = 2        # special case -> root's parent is itself (inode 2)

    # check reference count with num discovered links
    for inode in inodes:
        if int(inodeLinkCount[inode.num]) != int(inode.linksCount):
            print("INODE " + str(inode.num) + " HAS " + str(inodeLinkCount[inode.num]) + " LINKS BUT LINKCOUNT IS " + str(inode.linksCount))

    # check . and ..
    for dirent in dirents:
        if dirent.name == "'..'" and int(inodeToParentDict[dirent.parentInodeNum]) != int(dirent.inode):
            print("DIRECTORY INODE " + str(dirent.parentInodeNum) + " NAME '..' LINK TO INODE " + str(dirent.inode) + " SHOULD BE " + str(dirent.parentInodeNum))
        if dirent.name == "'.'" and int(dirent.parentInodeNum) != int(dirent.inode):
            print("DIRECTORY INODE " + str(dirent.parentInodeNum) + " NAME '.' LINK TO INODE " + str(dirent.inode) + " SHOULD BE " + str(dirent.inode))

    # for i in inodeToParentDict:
    #     print(inodeToParentDict[i])

if __name__ == "__main__":
    main()


import os, json
from django.shortcuts import render, redirect
from django.urls import reverse
from django.views import View
from braintree import Subscription
from braintree.error_result import ErrorResult
from app_auth import models as AuthModels
from app_auth import mixins as AuthMixins
from payments import models as PaymentModels
import datetime
from django.contrib import messages
from django.utils import timezone
from django.http import HttpResponseRedirect, HttpResponse

from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST

from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from django.conf import settings

from paypalrestsdk.notifications import WebhookEvent

from payments.utils.paypal import mode as mode

class PaymentView(View):
    template_name = "payments/index.html"
    context_data = dict()

# Create your views here.
class PaymentIndex(PaymentView):
    def get(self, request, *args, **kwargs):
        return render(request, self.template_name)

    def post(self, request, *args, **kwargs):
        pass

class CompletePayment(PaymentView):
    def get(self, request, *args, **kwargs):
        return redirect(reverse("manager:pricings"))

    def post(self, request, *args, **kwargs):
        pricing_paypal_id = request.POST.get("pricing_paypal_id")
        selected_plan = PaymentModels.PaypalPlan.objects.filter(paypal_id=pricing_paypal_id).first()
        current_pricing = PaymentModels.PaypalSubscription.objects.filter(user=request.user.id).first()

        subscription = PaymentModels.PaypalSubscription.objects.filter(user=request.user.id).first()
        try:
            if subscription.plan.name.lower() != "Basic Monthly Package".lower():
                ret = mode.myapi.post(f"v1/billing/subscriptions/{subscription.order_key}/suspend")
                if ret.get("error"):
                    messages.add_message(request, messages.ERROR, 'Opps! We are not able to cancel current subcription automatically.')
                    return redirect(reverse('manager:account'))
                else:
                    ret = mode.myapi.post(f"v1/billing/subscriptions/{subscription.order_key}/cancel")
                    messages.add_message(request, messages.ERROR, 'Opps! We are not able to cancel current subcription automatically.')
                    return redirect(reverse('manager:account'))

            data = {
                "plan_id": pricing_paypal_id
            }

            ret = mode.myapi.post("v1/billing/subscriptions", data)
            if ret.get("error"):
                messages.add_message(request, messages.ERROR, 'Unable to update your subscription. Contact support.')
                return redirect(reverse('manager:pricings'))

            if ret.get('status') == 'APPROVAL_PENDING':
                user = request.user

                subscription.previous_pricing = subscription.plan
                subscription.upgrading_to = selected_plan
                subscription.total_amount_paid=float(selected_plan.cost)
                subscription.order_key=ret['id']
                subscription.active=False

                subscription.expiry_date=timezone.now() + datetime.timedelta(days=30)

                subscription.save()

                redirect_url = mode.get_url_from(ret['links'], 'approve')

                return HttpResponseRedirect(redirect_url)
        except Exception as e:
            subscription.plan=current_pricing
            subscription.save()
            messages.add_message(request, messages.ERROR, 'Unable to update your subscription. Contact support.')
            return redirect(reverse('manager:pricings'))

def send_mail(email, message):
    email_body = render_to_string(
        "email_message.html",
        {
            "review": "{}".format(message),
        },
    )
    email = EmailMessage(
        "Ease Sell Extension",
        email_body,
        settings.DEFAULT_FROM_EMAIL,
        [
            email,
        ],
    )

    email.send(fail_silently=False)

@require_POST
@csrf_exempt
def paypal_webhooks(request):
    transmission_id = request.headers['Paypal-Transmission-Id']
    timestamp = request.headers['Paypal-Transmission-Time']
    webhook_id = os.environ.get("PAYPAL_WEBHOOK_ID")
    event_body = request.body.decode('utf-8')
    cert_url = request.headers['Paypal-Cert-Url']
    auth_algo = request.headers['Paypal-Auth-Algo']
    actual_signature = request.headers['Paypal-Transmission-Sig']

    response = WebhookEvent.verify(
        transmission_id,
        timestamp,
        webhook_id,
        event_body,
        cert_url,
        actual_signature,
        auth_algo
    )
    if response:
        obj = json.loads(request.body)
        event_type = obj.get('event_type')
        resource = obj.get('resource')

    try:

        if resource.get("status", None) == 'APPROVAL_PENDING':
            billing_agreement_id = resource['id']
            subscription = PaymentModels.PaypalSubscription.objects.filter(order_key=billing_agreement_id).first()
            email = AuthModels.User.objects.filter(id=subscription.user.id).first().email
            send_mail(email, "Your payment as been initialized. Please wait for confirmation email.")

        elif resource.get("status", None) == 'ACTIVE':

            billing_agreement_id = resource['id']
            subscription = PaymentModels.PaypalSubscription.objects.filter(order_key=billing_agreement_id).first()
            email = AuthModels.User.objects.filter(id=subscription.user.id).first().email
            send_mail(email, "Your payment was successfull.")

            subscription.plan = subscription.upgrading_to
            subscription.payment_completed = True
            subscription.active=True
            subscription.save()

        elif resource.get("status", None) == "SUSPENDED":
            billing_agreement_id = resource['id']
            subscription = PaymentModels.PaypalSubscription.objects.filter(order_key=billing_agreement_id).first()
            ret = mode.myapi.post(f"v1/billing/subscriptions/{subscription.order_key}/cancel")
            send_mail(email, "Ease sell subscription deactivated successfully.")

        elif event_type == "BILLING.SUBSCRIPTION.CANCELLED":
            billing_agreement_id = resource['id']
            subscription = PaymentModels.PaypalSubscription.objects.filter(order_key=billing_agreement_id).first()
            email = AuthModels.User.objects.filter(id=subscription.user.id).first().email
            send_mail(email, "Ease sell subscription deactivated successfully.")

        return HttpResponse(status=200)
    except:
        pass

@csrf_exempt
def PaypalSubscriptionDeactivateView(request):
    try:
        if request.method == 'POST':
            package = PaymentModels.PaypalSubscription.objects.filter(user=request.user).first()
            subscription = PaymentModels.PaypalSubscription.objects.filter(user=request.user.id).first()

            ret = mode.myapi.post(f"v1/billing/subscriptions/{subscription.order_key}/suspend")

            if ret.get("error"):
                messages.add_message(request, messages.ERROR, 'Opps! Unable to deactivate, Try Again.')
                return redirect(reverse('manager:account'))

            subscription.plan = PaymentModels.PaypalPlan.objects.filter(name="Basic Monthly Package").first()
            subscription.expiry_date=timezone.now()
            subscription.total_amount_paid=0
            subscription.order_key="Not Set"
            subscription.active=False
            subscription.save()
            return redirect(reverse('manager:account'))
        else:
            messages.add_message(request, messages.ERROR, 'Invalid Request.')
            return redirect(reverse('manager:home'))
    except Exception as E:
        messages.add_message(request, messages.ERROR, 'Please Try Again')
        return redirect(reverse('manager:home'))

class PaypalSubcribeView(PaymentView, AuthMixins.IsAuthenticatedAccessMixin):
    def post(self, request, *args, **kwargs):
        user = AuthModels.User.objects.filter(pk=request.user.pk)
        # create braintree customer

        # if user has not set first_name, last_name redirect to prompt page
        # stored the package he is paying for

        if not user.customer_payemnt_id:
            result = gateway.customer.create(
                {
                    "first_name": user.first_name,
                    "last_name": user.last_name,
                    "company": "EASE-SELL",
                    "email": user.email,
                    "phone": user.get_moblie_number(),
                }
            )
            if result.is_success:
                user.customer_payemnt_id = result.customer.id
                user.save

        customer = gateway.customer.find(user.customer_payemnt_id)

    def get(self, request, *args, **kwargs):
        try:
            braintree_client_token = braintree.ClientToken.generate(
                {"customer_id": self.request.user.id}
            )
        except:
            braintree_client_token = braintree.ClientToken.generate({})

        self.context_data["braintree_client_token"] = braintree_client_token

        return render(request, self.template_name, context=self.context_data)


#Simulador de caixa eletronico v2 kappa
s = c1 = v1 = d1 = u1 = resp = 0
num = int(input('Qual o valor deseja sacar? '))
while 50 <= num:
    num -= 50
    c1 += 1
    resp = (f'{c1} Cédulas de R$50')
if c1 >= 1:
    print(resp)
while 20 <= num:
    num -= 20
    v1 += 1
    resp = (f'{v1} Cédulas de R$20')
if v1 >= 1:
    print(resp)
while 10 <= num:
    num -= 10
    d1 += 1
    resp = (f'{d1} Cédulas de R$10')
if d1 >= 1:
    print(resp)
while 1 <= num:
    num -= 1
    u1 += 1
    resp = (f'{u1} Cédulas de R$1')
if u1 >= 1:
    print(resp)

import math
#Simulador de caixa eletrônico #Bruno
lista = [50, 20, 10, 1]
num = int(input('Qual o valor deseja sacar? '))
for valor_cedula in lista:
    total_cedula = math.floor(num / valor_cedula)
    print(num)
    num = num - total_cedula * valor_cedula
    if total_cedula > 0:
        print(f'{total_cedula},de R${valor_cedula}')

#Simulador de caixa eletrônico
c = 50
v = 20
d = 10
u = 1
s = c1 = v1 = d1 = u1 = 0
num = int(input('Qual o valor deseja sacar? '))
while s < num:
    s += c
    c1 += 1
    resp = f'{c1} Cédulas de R${c}'
    print(s)
if s > num:
    s = s - c
    c1 += - 1
    while s < num:
        s += v
        v1 += 1
        resp = f'{c1} Cédulas de R${c}\n{v1} Cédulas de R${v}\n'
    if s > num:
        s += - v
        v1 += -1
        while s < num:
            s += d
            d1 += 1
            resp = f'{c1} Cédulas de R${c}\n{v1} Cédulas de R${v}\n{d1} Cédulas de R${d}'
        if s > num:
            s += - d
            d1 += -1
            while s < num:
                s += u
                u1 += 1
                resp = f'{c1} Cédulas de R${c}\n{v1} Cédulas de R${v}\n{d1} Cédulas de R${d}\n{u1} Cédulas de R${u}'
print(resp)


import os

# event_type = "pulmonary"
event_type = "digest"

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

current_dir = os.path.dirname(os.path.abspath(__file__))  # 获取当前地址
proj_path = current_dir

weights_path = proj_path + "/weights"

##################################################################h

# Classification
train_file_path = proj_path + "/data/train_no_dump.txt"
val_file_path = proj_path + "/data/val_no_dump.txt"
test_file_path = proj_path + "/data/train_no_dump.txt"

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

# Model Config
MODEL_TYPE = 'albert'

BASE_MODEL_DIR = proj_path + "/albert_tiny_google_zh"
BASE_CONFIG_NAME = proj_path + "/albert_tiny_google_zh/albert_config.json"
BASE_CKPT_NAME = proj_path + "/albert_tiny_google_zh/albert_model.ckpt"


#!/usr/bin/env python3
import argparse

from help_functions import *
from sub_orgs import *
from sub_occs import *
from sub_arts import *
from qwikidata.json_dump import WikidataJsonDump
import sys
import time

"""
GENERAL
Hľadané atribúty:   id [ID]: samotny item
                    type [Type]: P2308 (trieda = qualifier to define a property constraint in combination with "property
                                 constraint")
                                 P31 (je = tato položka je jedna konretna vec exemplar)
                                 P279 (podtrieda = tento objekt je podtriedou...)
                    name [Name]: item["labels"]["cs"]["value"]
                    d_name [Display_name]: pri wikidata to reprezentuje ID
                    alias [Aliases]: v poli entity_d["aliases"]["cs"]
                    descrpt [Description]:  entity["descriptions"]["en/cs"]
                    roles [roles]: P2868 (subject has role)
                                 P3831 (object has role)
                    fictional [fictional]:  P1080 - (from narrative universe) subject's fictional entity is in the
                                                    object narrative.
                                                    See also P1441 (present in work) and P1445
                                            P1434 - (takes place in fictional universe) the subject is a work describing
                                                    a fictional universe,
                                                    i.e. whose plot occurs in this universe.
                                            P1074 - (fictional analog of) used to link an entity or class of entities
                                                    appearing in a creative
                                                    work with the analogous entity or class of entities in the real
                                                    world
                                            P1445 - (fictional universe described in) to link a fictional universe with
                                                    a work that
                                                    describes it: <universe> "described in the work:" <work>
                                            P1441 - (present in work) this (fictional) entity or person is present in
                                                    the story of that work
                                                    (use P2860 for works citing other works, P361/P1433
                                                    for works being part of / published in other works,
                                                    P1343 for entities described in non-fictional accounts)
                    wiki_url [Wikipedia URL]: Spojenim "https://cs.wikipedia.org/wiki/"
                                                                        +
                                                        entity_d["sitelinks"]["cswiki"]["title"]
                    wikidata_url [wikidata_url]:    https://www.wikidata.org/wiki/ID
                    dbpedia_url [dbpedia_url]       http://dbpedia.org/page/Nintendo/english_meno
                    imgs [Images]: P18 (obrazok = odkaz na subor)
                                   meno obrazku spolu z jeho hashom hladame v /mnt/data/kb/images/wikimedia/
                                   ak sa meno nenajde hladame ho na internete pomocou requests kniznice
                                   stiahneme ho a ulozime do prislusneho adresara pomocou hashu

Organisation
Hladane atributy:   inception [Inception]: P571 (cas kdy byl objekt zalozeny)
                    cancelled [Cancelled]: P576 (datum zaniku = datum kdy byla organizace rozpustena nebo
                    budova zborena, datum záaiku veci)
                    org_type [Organization type]: item["claims"]["P31"][0]["mainsnak"]["datavalue"]["value"]["id"]
                    location [Location]:   P625 (zemepisne suradnice)
                                           P276 (miesto = umiestnenie polozky predmetu ci udalosti)

Occurence
Hladane atributy:   beginning [Beginning]: P580 (start time = pociatok nejakeho stavu)
                    end [End]: P582 (end time = koniec nejakeho stavu)
                    location [Location]:   P625 (zemepisne suradnice)
                                           P276 (miesto = umiestnenie polozky predmetu ci udalosti)
"""

def parse_arguments():
    """
    Parses arguments from command line
    :return: True if all is ok and False otherwise
    """
    dump = "/mnt/minerva1/nlp/datasets/wikipedia/wikidata-20200727/wikidata-20200720-all.json"
    parser = argparse.ArgumentParser()
    parser.add_argument('-D', '--dump', default=dump, type=str, help='Path to json dump')
    parser.add_argument('-F', '--final', action='store_true', help='If calling from start_parsing.sh to save '
                                                                   'results in directory "final"')
    parser.add_argument('-T', '--test', action='store_true', help='If calling for testing and development purposes of '
                                                                  'ent_parser.py use this option')
    console_args = parser.parse_args()
    if console_args.final:
        return console_args.dump, "final/"
    elif console_args.test:
        return console_args.dump, "test/"
    else:
        return console_args.dump, ""

# ===============   Start of the main function   ===============

if __name__ == "__main__":
    wjd_dump_path, where = parse_arguments()
    # Create an instance of WikidataJsonDump
    # wjd_dump_path = "/mnt/minerva1/nlp/datasets/wikipedia/wikidata-20200803-all.json"
    wjd = WikidataJsonDump(wjd_dump_path)

    i = open("images", "w")
    i.close()
    orgs = []
    occs = []
    arts = []
    t1 = time.time()
    p = 0
    ii = 0
    dt = 0
    t = 0

    udalosti = open(where + "udalosti", "w", encoding='utf-8', newline='')
    organizace = open(where + "organizace", "w", encoding='utf-8', newline='')
    umelci = open(where + "vizual_umelci", "w", encoding='utf-8', newline='')
    l_f = where + "log_file"

    for ii, entity_dict in enumerate(wjd):
        try:
            get_atr(organizace, entity_dict, sub_of_organisation, orgs)
            get_atr(udalosti, entity_dict, sub_of_occurence, occs)
            get_atr(umelci, entity_dict, sub_of_artist, arts)
        except Exception as e:
            not_cs_label += 1

        if ii % 1000 == 0:
            t2 = time.time()
            dt = t2 - t1
            t = t1 - t2
            print(
                "{}s: found {} orgs and {} occs and {} arts and {} without cs label among {} entities [entities/s: {"
                ":.2f}]".format(
                    int(abs(t)), len(orgs), len(occs), len(arts), not_cs_label, ii, ii / dt
                )
            )
        if ii == 1000:
            break

    write_log_file(orgs, occs, arts, ii, not_cs_label, dt, t, l_f)
    udalosti.close()
    organizace.close()
    umelci.close()


from datetime import datetime
from subprocess import Popen, call
from threading import Thread
from time import sleep

class Alarm(Thread):
    def __init__(self, alarm_t=None, active=False):
        Thread.__init__(self)
        self.alarm_t = alarm_t
        self.active = active
        self._alarm_on = False

    def set_alarm(self, alarm_t):
        self.alarm_t = alarm_t
        self.active = True

    def activate(self):
        self.active = True

    def deactivate(self):
        self.active = False

    def run(self):
        while True:
            if self.active:
                if not self._alarm_on:
                    t = datetime.now()
                    if t.hour == self.alarm_t.hour and \
                       t.minute == self.alarm_t.minute:
                        play_spotify()
                        self._alarm_on = True
            else:
                if self._alarm_on:
                    stop_spotify()
                    self._alarm_on = False
            sleep(1)

    def time(self):
        return '{}:{}'.format(
            str(self.alarm_t.hour).zfill(2),
            str(self.alarm_t.minute).zfill(2)
        )

    def status(self):
        return "ON" if self.active else "OFF"

playlists = {
    'morning': 'spotify:user:1146628823:playlist:14FRsyt8MBES8xcNgSOdhy'
}

def start_spotify():
    p = Popen(['spotify'])
    if p.poll() is None:
        p.wait()

def play_spotify():
    playlist = playlists['morning']
    call(
        [
            'spotify-remote', 'play',
            playlist
        ],
    )

def stop_spotify():
    call(['spotify-remote', 'pause'])


#exec(open("midi4.py").read())
import tkinter
import os
import sys

def exitn():
    sys.exit("Finished")

def rungenerator():
    os.system("python3 midi4.py -m "+measurese.get()+" -swing "+swing.get()+" -sk "+snki.get()+" -c "+cymbal.get()+" -kick "+kicke.get()+" -snare "+snaree.get())
    finisht=tkinter.Label(main, font=("mincho", 12), text="the midi file has been\ngenerated successfully")
    finisht.grid(column=4, row=6, columnspan=3)
    exitb=tkinter.Button(main, text="Exit", command=exitn)
    exitb.grid(column=4, row=7)

main=tkinter.Tk()
main.title("Random drum grooves generator")
main.geometry('800x350')

text=tkinter.Label(main, font=("mincho", 13), height=5, text="This program generates random midi files of\nrandom drum grooves defined by the settings you enter")
text.grid(column=1, row=0, columnspan=4)

measurest=tkinter.Label(main, font=("mincho", 12), text="measures:", width=15, height=2)
measurest.grid(column=0, row=2)
measurese=tkinter.Entry(main, width=5)
measurese.grid(column=1, row=2)

swing=tkinter.StringVar()
swing.set("1")
swingt=tkinter.Label(main, font=("mincho", 12), text="swing pattern:", width=15, height=2)
swingt.grid(column=0, row=3)
swinge=tkinter.Radiobutton(main, text="Yes", value="1", variable=swing)
swinge.grid(column=1, row=3)
swinge=tkinter.Radiobutton(main, text="No", value=0, variable=swing)
swinge.grid(column=2, row=3)

cymbal=tkinter.StringVar()
cymbal.set("R")
cymbalt=tkinter.Label(main, font=("mincho", 12), text="cymbal type:", width=15, height=2)
cymbalt.grid(column=0, row=4)
cymbale=tkinter.Radiobutton(main, text="Ride", value="R", variable=cymbal)
cymbale.grid(column=1, row=4)
cymbale=tkinter.Radiobutton(main, text="Hi-Hat", value="HH", variable=cymbal)
cymbale.grid(column=2, row=4)

kickt=tkinter.Label(main, font=("mincho", 12), text="kick:", width=20)
kickt.grid(column=3, row=2)
kicke=tkinter.Entry(main, width=5)
kicke.grid(column=4, row=2)

snaret=tkinter.Label(main, font=("mincho", 12), text="snare:", width=20)
snaret.grid(column=3, row=3)
snaree=tkinter.Entry(main, width=5)
snaree.grid(column=4, row=3)

snki=tkinter.StringVar()
snki.set("0")
snkit=tkinter.Label(main, font=("mincho", 12), text="snare and kick\ncollision:", width=15, height=2)
snkit.grid(column=3, row=4)
snkie=tkinter.Radiobutton(main, text="Yes", value="1", variable=snki)
snkie.grid(column=4, row=4)
snkie=tkinter.Radiobutton(main, text="No", value="0", variable=snki)
snkie.grid(column=5, row=4)

btg = tkinter.Button(main, text="Generate", command=rungenerator)
btg.grid(column=6, row=5)

main.mainloop()


# -*- coding: utf-8 -*-
__author__ = 'JLPlata'

from bs4 import BeautifulSoup
import requests

#ingrso la url
URL_BASE = "https://www.scoresandodds.com"

MAX_PAGES = 20
counter = 0

for i in range(1, MAX_PAGES):

    # Construyo la URL
    if i > 1:
        url = "%spage/%d/" % (URL_BASE, i)
    else:
        url = URL_BASE

    # Realizamos la petición a la web
    req = requests.get(url)
    # Comprobamos que la petición nos devuelve un Status Code = 200
    statusCode = req.status_code
    if statusCode == 200:

        # Pasamos el contenido HTML de la web a un objeto BeautifulSoup()
        html = BeautifulSoup(req.text, "html.parser")

        # se busca mediante segun el elemetno (class, div, p, ...)
        entradas = html.find('aside', {'class': 'page-sidebar'})

        print (entradas)

    else:
        print("")
        break


"""musiclearn_cli.py
Command line interface for the musiclearn project.
Authors: Alex Kyllo and Jack Phan
"""
import logging
import os
import random
from pathlib import Path

import click
import numpy as np
from tensorflow.random import set_seed

from musiclearn import (plotting, processing, sequential_models,
                        single_note_processing, training, vae_models)

LOG = logging.getLogger("musiclearn")
LOG.setLevel(logging.DEBUG)
fh = logging.FileHandler("musiclearn.log")
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
fh.setFormatter(formatter)
LOG.addHandler(fh)

def set_seeds(seed):
    """Set random seed for Python, NumPy and TensorFlow"""
    np.random.seed(seed)
    set_seed(seed)
    random.seed(seed)

@click.command()
@click.argument("exp_name", type=click.STRING)
@click.option("--ticks-per-beat", type=click.INT, default=4, help="Time steps per quarter note.")
@click.option("--beats-per-phrase", type=click.INT, default=4, help="Quarter notes per phrase.")
@click.option("--epochs", type=click.INT, default=500, help="The training batch size.")
@click.option("--batch-size", type=click.INT, default=32, help="The training batch size.")
@click.option(
    "--learning-rate", type=click.FLOAT, default=0.0002, help="The optimizer learning rate."
)
@click.option("--lstm-units", type=click.INT, default=256, help="Number of LSTM units per layer.")
@click.option("--latent-dim", type=click.INT, default=100, help="The latent vector dimension.")
@click.option("--embedding-dim", type=click.INT, default=8, help="The note embedding dimension.")
@click.option(
    "--dropout-rate", type=click.FLOAT, default=0.4, help="The dropout rate between LSTM layers"
)
@click.option("--gru/--lstm", default=False, help="Use GRU layer instead of LSTM.")
@click.option(
    "--bidirectional/--unidirectional",
    default=False,
    help="Use bidirectional LSTM layer in encoder.",
)
@click.option(
    "--augment/--no-augment",
    default=False,
    help="Augment the training set with random pitch shifts.",
)
@click.option("--patience", type=click.INT, default=10, help="The early stopping patience.")
def fit_mtvae(
    exp_name,
    ticks_per_beat,
    beats_per_phrase,
    epochs,
    batch_size,
    learning_rate,
    lstm_units,
    latent_dim,
    embedding_dim,
    dropout_rate,
    gru,
    bidirectional,
    patience,
    augment,
):
    """Run MultiTrackVAE experiment named EXP_NAME with hyperparameter options.
    Author: Alex Kyllo"""
    training.train_mtvae(
        exp_name,
        vae_models.MultiTrackVAE,
        ticks_per_beat,
        beats_per_phrase,
        epochs,
        batch_size,
        learning_rate,
        lstm_units,
        latent_dim,
        embedding_dim,
        dropout_rate,
        gru,
        bidirectional,
        patience,
        augment,
    )

@click.command()
@click.argument("model_path", type=click.Path(exists=True))
@click.argument("midi_1", type=click.Path(exists=True))
@click.argument("midi_2", type=click.Path(exists=True))
@click.argument("output_dir", type=click.Path())
@click.option("--n", type=click.INT, default=3)
def interpolate_mtvae(model_path, midi_1, midi_2, output_dir, n):
    """Use MODEL_PATH to interpolate n points between MIDI_1 and MIDI_2."""
    programs = [40, 40, 41, 42]  # violin x2, viola, cello
    model = vae_models.MultiTrackVAE.from_saved(model_path)
    scores = [processing.midi_to_music21(f) for f in [midi_1, midi_2]]
    arrays = [processing.score_to_array(sc, model.ticks_per_beat) for sc in scores]
    output_arrays = model.interpolate(arrays[0], arrays[1], n)
    output_scores = [
        processing.array_to_score(arr, programs=programs, resolution=model.ticks_per_beat)
        for arr in output_arrays
    ]
    LOG.info(
        f"Generating {n} interpolations between {midi_1} and {midi_2} and saving to {output_dir}..."
    )
    os.makedirs(output_dir, exist_ok=True)
    for i, score in enumerate(output_scores):
        score.write("midi", Path(output_dir) / f"interpolation_{i}.mid")

@click.command()
@click.option(
    "--model-type",
    type=click.Choice(["lstm", "bidirect", "att", "wavenet"], case_sensitive=False),
    help="Type of model",
)
@click.option("--data-path", type=click.STRING, help="Path to folder stores dataset")
@click.option("--logs-dir", type=click.STRING, help="Folder that stores training logs")
@click.option(
    "--sequence_length",
    type=click.INT,
    default=100,
    help="Length of the sequences of notes used in training",
)
def fit_sequential(model_type, data_path, logs_dir, sequence_length):
    """Fit a sequential model of choice on the specified dataset.
    Author: Jack Phan"""
    notes = single_note_processing.read_midi(data_path)

    n_vocab = single_note_processing.get_num_unique_notes(notes)

    network_input, network_output = single_note_processing.prepare_sequences(sequence_length, notes)

    if model_type == "lstm":
        model = sequential_models.lstm_model(network_input.shape, n_vocab)
    elif model_type == "bidirect":
        model = sequential_models.bidirectional_lstm_model(network_input.shape, n_vocab)
    elif model_type == "att":
        model = sequential_models.attention_lstm_model(network_input.shape, n_vocab)
    elif model_type == "wavenet":
        model = sequential_models.simplified_wavenet(network_input.shape, n_vocab)
    else:
        raise ValueError("Invalid model type")

    sequential_models.train_model(model, sequence_length, model_type, 10, 64)

@click.command()
@click.argument("output-name", type=click.STRING)
@click.option("--data-path", type=click.STRING, help="Path to folder stores dataset")
@click.option(
    "--model-type",
    type=click.Choice(["lstm", "bidirect", "att", "wavenet"], case_sensitive=False),
    help="Type of model",
)
@click.option("--weights-path", type=click.STRING, help="Path to saved weights of the model")
@click.option("--num-notes", type=click.INT, help="Number of notes to generate")
def generate_music(output_name, data_path, model_type, weights_path, num_notes):
    """Generate a short piece of music with a fixed number of notes.
    Author: Jack Phan"""
    # load the model
    notes = single_note_processing.read_midi(data_path)
    n_vocab = single_note_processing.get_num_unique_notes(notes)
    network_input, _ = single_note_processing.prepare_sequences(
        sequential_models.SEQUENCE_LENGTH, notes
    )
    if model_type == "lstm" or model_type == "bidirect":
        model = sequential_models.load_lstm_model(weights_path)
    elif model_type == "att":
        model = sequential_models.load_attention_lstm_model(
            weights_path, network_input.shape, n_vocab
        )
    elif model_type == "wavenet":
        model = sequential_models.load_wavenet_model(weights_path)
    else:
        raise ValueError("Invalid model type")

    sequential_models.generate_midi_sample(model, data_path, output_name, num_notes)

@click.command()
@click.argument("history_file", type=click.Path(exists=True))
@click.argument("output_file", type=click.Path())
def plot_losses(history_file, output_file):
    """Plot model training & validation loss curves from HISTORY_FILE and save to OUTPUT_FILE."""
    os.makedirs(os.path.dirname(output_file), exist_ok=True)
    plotting.plot_learning_curves(history_file, output_file)

@click.group()
def cli():
    """Command line interface for the musiclearn project"""

def main():
    """"""
    cli.add_command(fit_mtvae)
    cli.add_command(interpolate_mtvae)
    cli.add_command(fit_sequential)
    cli.add_command(generate_music)
    cli.add_command(plot_losses)
    cli()

if __name__ == "__main__":
    SEED = 1337
    set_seeds(SEED)
    main()


def find(ss):
    if len(ss) == 0:
        return None
    if len(ss) == 1:
        return ss
    else:
        result = []
        for i in range(0, len(ss)):
            if i == 0 or ss[i] != ss[0]:
                tmp = ss[i]
                ss[i] = ss[0]
                ss[0] = tmp
                new_result = find(ss[1:])
                for new in new_result:
                    new = ss[0] + new
                    result.append(new)
                tmp = ss[i]
                ss[i] = ss[0]
                ss[0] = tmp
        return result
class Solution:
    def Permutation(self, ss):
        if len(ss) == 0:
            return []
        else:
            ss = list(ss)
            result = find(ss)
            result.sort()
            return result

if __name__ == '__main__':
    s = Solution()
    x = "12345"
    x = "abc"
    a = s.Permutation(x)
    print(a)
    print(type(a[0]))


from collections import defaultdict
from queue import *
from math import floor, ceil
import matplotlib.pyplot as plt
import random
from itertools import cycle
import subprocess
import re

netlistFilePath		= 'netlist.txt'
placementFilePath 	= 'cell_locs_pre_leg.txt'
defFile 			= 'cts.def'
verilogFile			= 'place.v'

nets 			= defaultdict(list) # these are nets with dummy buffers
netsPostProc	= defaultdict(list) # these are nets wo/ dummy buffers
placement 		= defaultdict(list)
buffers			= defaultdict(list)
segments		= defaultdict(list)

inClkPins	     = set()
useTrailingChar  = False
numSinglePinNets = 0

#------------------------------------------------------------------------------
# Read netlist from locations.txt file
def readNetlistFile():
	with open(netlistFilePath) as fp:
		for line in fp:
			terms = line.rstrip("\n").replace("{", "").replace("}", "").split(' ')
			if terms[0] is "B":
				for i in range(2, len(terms)):
					instPinPair = terms[i].rsplit("/", 1)
					inClkPins.add(instPinPair[1])
					nets[terms[1]].append([instPinPair[0], instPinPair[1]])
			else:
				node 	= "ck_" + terms[0]
				libCell	= terms[1]
				inNet 	= terms[2]
				outNet	= terms[3]
				nets[inNet].append([node, "A"])
				nets[outNet].insert(0, [node, "_BUFF_OUT_PIN_"])
				buffers[node] = [libCell, inNet, outNet]

#------------------------------------------------------------------------------
# Read placement from CT-related components locations file
def readPlacementFile():
	global useTrailingChar
	with open(placementFilePath) as fp:
		for line in fp:
			terms = line.rstrip("\n").split(' ')
			useTrailingChar = useTrailingChar or ("\\" in terms[0])
			placement[terms[0]].append(terms[1])
			placement[terms[0]].append(terms[2])

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

def writeNets():
	print("- _CK_PORT_")
	print("( PIN _CK_PORT_ )")
	print("( ck_tree_0 A )")
	print(";")

	global useTrailingChar
	for net, components in netsPostProc.items():
		if len(components) < 2:
			continue

		print("- " + net)
		for node, pin in components:
			if useTrailingChar and "/" in node and "\\/" not in node:
				node = node.replace("/", "\\/")

			if useTrailingChar and "[" in node and "\\[" not in node:
				node = node.replace("[", "\\[")

			if useTrailingChar and "]" in node and "\\]" not in node:
				node = node.replace("]", "\\]")

			print("( " + node + " " + pin + " )")
		print(";")

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

def postProcNets():
	toVisit = Queue()
	toVisit.put("ck_tree_0")

	while not toVisit.empty():
		currNode = toVisit.get()
		#print ("Curr node: " + currNode)

		netsToVisit = Queue()
		rootNet = buffers[currNode][2]
		netsPostProc[rootNet].append([currNode, '_BUFF_OUT_PIN_'])
		netsToVisit.put(rootNet)

		while not netsToVisit.empty():
			currNet = netsToVisit.get()
			driver, dPin = nets[currNet][0]
			for i in range(1, len(nets[currNet])):
				node, pin = nets[currNet][i]

				#segments for routing guides
				if node in buffers:
					segments[rootNet].append([placement[driver], placement[node]])

				#print("\tTesting sink: " + node)
				if not node in buffers:
					#print("\tSink: " + node)
					netsPostProc[rootNet].append([node, pin])
				elif "DUMMY" in buffers[node][0]:
					#print("\t\tis Dummy!")
					netsToVisit.put(buffers[node][2])
				else:
					#print("\tSink: " + node)
					netsPostProc[rootNet].append([node, pin])
					toVisit.put(node)
		#print("-----------------------------")

	global numSinglePinNets
	for net, sinks in netsPostProc.items():
		if len(sinks) == 1:
			numSinglePinNets += 1

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

def writeComponents():
	for buf, data in buffers.items():
		libCell = data[0]
		if "DUMMY" in libCell:
			continue
		x = int(float(placement[buf][0]) * 1000)
		y = int(float(placement[buf][1]) * 1000)
		print(" - " + buf + " " + libCell + " + PLACED ( " + str(x) + " " + str(y) + " ) N\n;")

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

def dumpVerilog():
	inputs = defaultdict(str)
	outputs = defaultdict(str)
	for net, components in netsPostProc.items():
		if len(components) < 2:
			outputs[components[0][0]] = "dangling"
			continue

		outputs[components[0][0]] = net
		for i in range(1, len(components)):
			if (components[i][1]) in inClkPins:
				inputs[components[i][0] + "/" + components[i][1]] = net
			else:
				inputs[components[i][0]] = net

	inputs['ck_tree_0'] = '_CK_PORT_'
	printedNets = False
	outputfile = open('final.v', 'w')
	with open('place-2.v') as fp:
		for line in fp:
			line = re.sub("\t", " ", line)
			line = re.sub("\s+", " ", line)
			if "wire" in line and not printedNets:
				for net, components in netsPostProc.items():
					outputfile.write("  wire " + net + ";\n")
				printedNets = True
			elif "endmodule" in line:
				for mod, out in outputs.items():
					libcell = buffers[mod][0]
					if outputs[mod] == "dangling":
						outputfile.write("  " + libcell + " " + mod + "( .A(" + inputs[mod] + ") );\n")
					else:
						outputfile.write("  " + libcell + " " + mod + "( .A(" + inputs[mod] + "), ._BUFF_OUT_PIN_(" + outputs[mod]  + ") );\n")
				outputfile.write("endmodule")
			else:
				for clkPin in inClkPins:
					pin = "." + clkPin
					if pin in line:
						line = re.sub(pin + "\s*\(\s*_CK_PORT_\s*\)", pin + "(" + inputs[line.split()[1].replace("\\", "") + "/" + clkPin] + ")", line)
				outputfile.write(line + "\n")
	outputfile.close()

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

def computeNumBuffers():
	numBuff = 0
	for buf, data in buffers.items():
		libCell = data[0]
		if "DUMMY" in libCell:
			continue
		numBuff = numBuff + 1
	return numBuff

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

### main
readNetlistFile()
readPlacementFile()
postProcNets()
dumpVerilog()

cmd0 = "grep \"COMPONENTS \" place.def | tail -1 | awk '{gsub(/[^0-9. ]/,\"\")}1'"
cmd1 = "grep \"NETS \" place.def | tail -1 | awk '{gsub(/[^0-9. ]/,\"\")}1'"

with open(defFile) as fp:
	while True:
		line = fp.readline()
		if not line:
			break
		line = line.rstrip("\n")
		terms = line.split(" ")

		if "- ck_" in line:
			while ";" not in line:
				line = fp.readline();
			continue
		elif "COMPONENTS" in line and "END" not in line:
			terms = line.split(" ")
			numComponents = int(subprocess.check_output(cmd0, shell=True)) + computeNumBuffers()
			print("COMPONENTS " + str(numComponents) + " ;")
			writeComponents()
		elif "NETS" in line and "END" not in line and "SPECIAL" not in line:
			terms = line.split(" ")
			numNets = int(subprocess.check_output(cmd1, shell=True)) + len(netsPostProc) - numSinglePinNets
			print("NETS " + str(numNets) + " ;")
			writeNets()
		elif "- _CK_PORT_" in line and not "NET" in line: # Skip the original clock net
			while ";" not in line:
				line = fp.readline()
		else:
			print(line)


#!/usr/bin/python3
#
# ADXL345.py
#
# Created on: April 27, 2021
# Author: LongHD
#
# ACCEL sensor
# Reference https://github.com/jakalada/Arduino-ADXL345
# http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf
# http://www.analog.com/media/jp/technical-documentation/data-sheets/ADXL345_jp.pdf
#

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

import math
from time import sleep
from i2c.i2c import I2C

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

ADXL345_I2C_ADDRESS                       = 0x53 # The device i2c address in default

# Please refer to the sensor datasheet.
# Register Map
# Order is "Type, Reset Value, Description".
REG_DEVID                                 = 0x00    # R,     11100101,   Device ID
REG_THRESH_TAP                            = 0x1D    # R/W,   00000000,   Tap threshold
REG_OFSX                                  = 0x1E    # R/W,   00000000,   X-axis offset
REG_OFSY                                  = 0x1F    # R/W,   00000000,   Y-axis offset
REG_OFSZ                                  = 0x20    # R/W,   00000000,   Z-axis offset
REG_DUR                                   = 0x21    # R/W,   00000000,   Tap duration
REG_LATENT                                = 0x22    # R/W,   00000000,   Tap latency
REG_WINDOW                                = 0x23    # R/W,   00000000,   Tap window
REG_THRESH_ACT                            = 0x24    # R/W,   00000000,   Activity threshold
REG_THRESH_INACT                          = 0x25    # R/W,   00000000,   Inactivity threshold
REG_TIME_INACT                            = 0x26    # R/W,   00000000,   Inactivity time
REG_ACT_INACT_CTL                         = 0x27    # R/W,   00000000,   Axis enable control for activity and inactiv ity detection
REG_THRESH_FF                             = 0x28    # R/W,   00000000,   Free-fall threshold
REG_TIME_FF                               = 0x29    # R/W,   00000000,   Free-fall time
REG_TAP_AXES                              = 0x2A    # R/W,   00000000,   Axis control for single tap/double tap
REG_ACT_TAP_STATUS                        = 0x2B    # R,     00000000,   Source of single tap/double tap
REG_BW_RATE                               = 0x2C    # R/W,   00001010,   Data rate and power mode control
REG_POWER_CTL                             = 0x2D    # R/W,   00000000,   Power-saving features control
REG_INT_ENABLE                            = 0x2E    # R/W,   00000000,   Interrupt enable control
REG_INT_MAP                               = 0x2F    # R/W,   00000000,   Interrupt mapping control
REG_INT_SOUCE                             = 0x30    # R,     00000010,   Source of interrupts
REG_DATA_FORMAT                           = 0x31    # R/W,   00000000,   Data format control
REG_DATAX0                                = 0x32    # R,     00000000,   X-Axis Data 0
REG_DATAX1                                = 0x33    # R,     00000000,   X-Axis Data 1
REG_DATAY0                                = 0x34    # R,     00000000,   Y-Axis Data 0
REG_DATAY1                                = 0x35    # R,     00000000,   Y-Axis Data 1
REG_DATAZ0                                = 0x36    # R,     00000000,   Z-Axis Data 0
REG_DATAZ1                                = 0x37    # R,     00000000,   Z-Axis Data 1
REG_FIFO_CTL                              = 0x38    # R/W,   00000000,   FIFO control
REG_FIFO_STATUS                           = 0x39    # R,     00000000,   FIFO status

# Data Rate
ADXL345_RATE_3200HZ                       = 0x0F    # 3200 Hz
ADXL345_RATE_1600HZ                       = 0x0E    # 1600 Hz
ADXL345_RATE_800HZ                        = 0x0D    # 800 Hz
ADXL345_RATE_400HZ                        = 0x0C    # 400 Hz
ADXL345_RATE_200HZ                        = 0x0B    # 200 Hz
ADXL345_RATE_100HZ                        = 0x0A    # 100 Hz
ADXL345_RATE_50HZ                         = 0x09    # 50 Hz
ADXL345_RATE_25HZ                         = 0x08    # 25 Hz
ADXL345_RATE_12_5HZ                       = 0x07    # 12.5 Hz
ADXL345_RATE_6_25HZ                       = 0x06    # 6.25 Hz
ADXL345_RATE_3_13HZ                       = 0x05    # 3.13 Hz
ADXL345_RATE_1_56HZ                       = 0x04    # 1.56 Hz
ADXL345_RATE_0_78HZ                       = 0x03    # 0.78 Hz
ADXL345_RATE_0_39HZ                       = 0x02    # 0.39 Hz
ADXL345_RATE_0_20HZ                       = 0x01    # 0.20 Hz
ADXL345_RATE_0_10HZ                       = 0x00    # 0.10 Hz

# Range
ADXL345_RANGE_2G                          = 0x00    # +-2 g
ADXL345_RANGE_4G                          = 0x01    # +-4 g
ADXL345_RANGE_8G                          = 0x02    # +-8 g
ADXL345_RANGE_16G                         = 0x03    # +-16 g

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

class ADXL345:
    # Sensor with default address
    def __init__(self, i2c, address = ADXL345_I2C_ADDRESS, srange = ADXL345_RANGE_16G):
        self.__i2c = i2c
        self.__address = address
        self.__range = srange

        self.set_range(srange)
        self.set_rate(ADXL345_RATE_200HZ)
        self.config_tap_detect()
        self.start()

    # Write register
    def __write_reg(self, reg, value):
        self.__i2c.i2c_write_block_data(self.__address, reg, [value])

    # Read bytes from register
    def __read_reg(self, reg, size):
        self.__i2c.i2c_write_byte(self.__address, reg)
        return self.__i2c.i2c_read_data(self.__address, size)

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

    # Convert value to SI
    def convert(self, value):
        res = 0
        if self.__range == ADXL345_RANGE_2G:
            res = 2
        elif self.__range == ADXL345_RANGE_4G:
            res = 4
        elif self.__range == ADXL345_RANGE_8G:
            res = 8
        elif self.__range == ADXL345_RANGE_16G:
            res = 16
        else:
            return 0

        return (res * value * 2) / 1024.0

    # Set data rate
    # See constant "Data rate"
    # Ex: ADXL345_RATE_3200HZ
    #     ...
    #     ADXL345_RATE_0_10HZ
    def set_rate(self, rate):
        # Low power = 0
        self.__write_reg(REG_BW_RATE, rate & 0xFF)

    # Set range
    # See constant "Range"
    # +-2 g, +-4 g, +-8 g, +-16 g
    def set_range(self, srange):
        # range 0 - 3
        self.__write_reg(REG_DATA_FORMAT, srange & 0x03)
        self.__range = srange

    # Get device id
    # Can used for check device available
    # Default id: 0xE5 (229)
    def get_device_id(self):
        return self.__read_reg(REG_DEVID, 1)[0]

    def config_tap_detect(self):
        self.__write_reg(REG_TAP_AXES, 0x07)     # enable tap detection on XYZ axis
        self.__write_reg(REG_THRESH_TAP, 0x28)   # set tap threshold 2.5g/.0625
        self.__write_reg(REG_DUR, 0x20)          # set tap duration .02 sec/.000625
        self.__write_reg(REG_LATENT, 0x50)       # double tap latency .1sec/.00125
        self.__write_reg(REG_WINDOW, 0xF0)       # double tap window .3sec/.00125
        self.__write_reg(REG_INT_ENABLE, 0x60)   # int enable
        self.__write_reg(REG_INT_MAP, 0x00)      # int map reg 0 means INT1

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

    # Start measure value
    # Disable auto sleep
    # See datasheet for more detail
    def start(self):
        # bit measure = 1
        self.__write_reg(REG_POWER_CTL, 0x01 << 3)

    # Stop measure value
    # Reset to default
    def stop(self):
        # bit measure = 0
        self.__write_reg(REG_POWER_CTL, 0x00)

    # Get xyz values
    # See "Register 0x32 to Register 0x37—DATAX0, DATAX1,DATAY0, DATAY1, DATAZ0, DATAZ1 (Read Only)" in datasheet
    # DATAx0 as the least significant byte and DATAx1 as the most significant byte
    # Return value of ax, ay, az (unit g (1g = 9.8 m/s^2))
    # Normal x = 0g, y = 0g, z = 1g
    def get_accel(self):
        read = self.__read_reg(REG_DATAX0, 6)
        # Little endian
        x = int.from_bytes([read[0], read[1]], byteorder = 'little', signed = True)
        y = int.from_bytes([read[2], read[3]], byteorder = 'little', signed = True)
        z = int.from_bytes([read[4], read[5]], byteorder = 'little', signed = True)

        return self.convert(x), self.convert(y), self.convert(z)

    # Get tap status
    # Return Single Tap, Double Tap or None
    def get_tap(self):
        read = self.__read_reg(REG_INT_SOUCE, 1)[0]
        if ((read >> 5) & 0x01):
            return "Double Tap"
        elif ((read >> 6) & 0x01):
            return "Single Tap"
        else:
            return None

    # Get Tilt angle
    # x, y, z: accel x, y, z
    # Return x, y, z angle in radian
    # https://forum.arduino.cc/t/how-to-get-degrees-of-rotation-from-raw-acceleration-data/451992/11
    # https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing
    def get_tilt_angle(self, x, y, z):
        if y == 0 and z == 0 and x >= 0:
            ax = math.pi / 2
        elif y == 0 and z == 0 and x < 0:
            ax = -math.pi / 2
        else:
            ax = math.atan(x / math.sqrt(pow(y, 2) + pow(z, 2)))

        if x == 0 and z == 0 and y >= 0:
            ay = math.pi / 2
        elif x == 0 and z == 0 and y < 0:
            ay = -math.pi / 2
        else:
            ay = math.atan(y / math.sqrt(pow(x, 2) + pow(z, 2)))

        if z == 0:
            az = 0
        else:
            az = math.atan(math.sqrt(pow(x, 2) + pow(y, 2)) / z)

        return ax, ay, az

    # Get direction
    # Return up (z max), down (z min), right (x max), left (x min), front (y max), back (y min)
    def get_direction(self, x, y, z):
        THRESHOLD = 0.3

        if x > THRESHOLD and x > y and x > z:
            return "Right"
        elif x < -THRESHOLD and x < y and x < z:
            return "Left"

        elif y > THRESHOLD and y > x and y > z:
            return "Front"
        elif y < -THRESHOLD and y < x and y < z:
            return "Back"

        elif z > THRESHOLD and z > x and z > y:
            return "Up"
        elif z < -THRESHOLD and z < x and z < y:
            return "Down"

        else:
            return "Unknow"

    # Get sensor is vibration or not
    # Time sleep between 2 measure depends on data rate
    # Return true if is vibration
    def is_vibration(self):
        values1 = list(self.get_accel())
        sleep(0.1)  # wait new sample
        values2 = list(self.get_accel())
        sleep(0.1)  # wait new sample
        values3 = list(self.get_accel())

        delta = 0
        for i in range(3):
            delta += abs(values2[i] - values1[i]) + abs(values3[i] - values2[i])

        return delta > 0.5

#-------------------------- Example --------------------------

"""
i2c = I2C()
adxl345 = ADXL345(i2c)
while True:
    x, y, z = adxl345.get_accel()
    print(x, y, z)
    # print(adxl345.get_direction(x, y, z))
    # print(adxl345.is_vibration())

    # tap = adxl345.get_tap()
    # if tap != None:
    #     print(tap)
    # sleep(0.3)   # must wait next tap

    print(adxl345.get_tilt_angle(x, y, z))
    sleep(0.5)

adxl345.stop()
"""


import json, os
import numpy as np
import torch
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
from torch.utils.data import Dataset
from glob import glob
import torchvision.transforms as T

def read_mask(label_path):
    label_data = json.load(open(label_path))
    segs = label_data["shapes"][0]["points"]
    img_shape = [label_data["imageHeight"], label_data["imageWidth"]]
    mask = np.zeros(img_shape, dtype=np.uint8)
    mask_image = Image.fromarray(mask)
    xy = list(map(tuple, segs))
    ImageDraw.Draw(mask_image).polygon(xy=xy, outline=1, fill=255)
    mask_image = np.array(mask_image)
    mask = mask_image.astype(np.int32)
    return mask

class ForceData(Dataset):
    def __init__(self, data_path) -> None:
        super().__init__()
        self.force = np.loadtxt(open(data_path+"/data.csv","r"),delimiter=",")
        self.rgb = glob(os.path.join(data_path, "rgb_cut", "*.jpg"))
        self.inf = glob(os.path.join(data_path, "inf_cut", "*"))
        self.lable = glob(os.path.join(data_path, "rgb_cut", "*.json"))
        self.rgb.sort()
        self.inf.sort()
        self.lable.sort()

        self.scale = [2,2,6]

        self.transform1 = T.Compose([
            T.Resize(256),
            T.ToTensor(),
            T.Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225]
            )
        ])
        self.transform2 = T.Compose([
            T.ToPILImage(),
            T.Resize(256),
            T.ToTensor()
        ])

    def __getitem__(self, index):
        rgb = Image.open(self.rgb[index])
        inf = Image.open(self.inf[index])

        x_force = torch.tensor((self.force[index][0] + self.scale[0] / 2) / self.scale[0])
        y_force = torch.tensor((self.force[index][1] + self.scale[1] / 2) / self.scale[1])
        z_force = torch.tensor((self.force[index][2] + self.scale[2] / 2) / self.scale[2])

        rgb = self.transform1(rgb)
        inf = self.transform1(inf)

        return rgb.float(), inf.float(), (x_force.float(), y_force.float(), z_force.float())

    def __len__(self):
        return len(self.rgb)


# UI library based on `uibase`

import uibase

get_handle = uibase.handle
clear_screen = uibase.cls

def welcome(main_program_version: str, hd: uibase.handle):
    uibase.title('Consoledit v{}'.format(main_program_version))
    hd.reset_screen_buffer_size()
    hd.free_focus(7, 35)
    print('Welcome to')
    hd.free_focus(8, 34)
    print('[Consoledit]')
    hd.free_focus(10, 33)
    print('Drag file here')
    hd.free_focus(11, 31)
    print('or enter file path')
    hd.free_focus(12, 36)
    print('to open')
    hd.free_focus(13, 31)
    print('Enter \'x\' to exit.')

def get_command():
    get_handle().clear_buffer(23)
    get_handle().focus(23)
    command = input('> ').rstrip()
    return command

def status_bar(anchor: int, buffers: [['stripped']]):
    get_handle().clear_buffer(24, page_end=True)
    get_handle().focus(24)
    get_handle().write('$$---{} screen of {}---$$'.format(anchor+1, len(buffers)).rstrip())

def custom_status_bar(text: str):
    get_handle().clear_buffer(24, page_end=True)
    get_handle().focus(24)
    get_handle().write(text)

def analysis_command(command: str, buffer_length: int):
    if command.isdigit():
        if len(command) == 2 and 1 <= int(command) <= buffer_length:
            buffer_number = int(command) - 1
            return 'edit', buffer_number
        else:
            return 'invalid', 'Invalid line-number'
    elif command == 'i' and command.isdigit():
        if len(command) == 2 and 1 <= int(command) <= buffer_length:
            buffer_number = int(command) - 1
            return 'insert', buffer_number
        else:
            return 'invalid', 'Invalid line-number'
    elif command == 'r' and command.isdigit():
        if len(command) == 2 and 1 <= int(command) <= buffer_length:
            buffer_number = int(command) - 1
            return 'replace', buffer_number
        else:
            return 'invalid', 'Invalid line-number'
    elif command == 's':
        return 'save',
    elif command in ['w', 'd']:
        return 'scroll', command
    elif command == 'x':
        return 'exit',
    else:
        return 'invalid', 'Invalid command'

def show_buffer(buffer: ['stripped']):
    for i, each in enumerate(buffer):
        get_handle().clear_buffer(i)
        get_handle().focus(i)
        line_number = i + 1
        if line_number < 10:
            buffer_number = '0' + str(line_number)
        else:
            buffer_number = str(line_number)
        print('<{}> '.format(buffer_number), end='')
        if len(each) >= 75:
            print(each[:72]+'...')
        else:
            print(each)

def clear_buffer():
    for i in range(20):
        get_handle().clear_buffer(i)

def dispatch_buffer(file_lines: ['stripped']):
    buffers = []
    for i, each in enumerate(file_lines):
        if i % 20 == 0:
            buffers.append([each])
        else:
            buffers[i//20].append(each)
    return buffers

def recover_buffer(buffers: [['stripped']]):
    lines = []
    for each in buffers:
        lines.extend(each)
    return lines

'''
h = get_handle()
welcome('0.1', h)
h.focus(23)
input('> ')
'''