# -*- coding: utf-8 -*-
import numpy as np
import os
import time
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torchvision.transforms as trn
import torchvision.datasets as dset
import torch.nn.functional as F
import json
from attack_methods import pgd
from models.wrn import WideResNet
from option import BaseOptions

class PrivateOptions(BaseOptions):
    def initialize(self):
        BaseOptions.initialize(self)

        # WRN Architecture
        self.parser.add_argument('--layers', default=28, type=int, help='total number of layers')
        self.parser.add_argument('--widen-factor', default=10, type=int, help='widen factor')
        self.parser.add_argument('--droprate', default=0.0, type=float, help='dropout probability')

# /////////////// Training ///////////////

def train():
    net.train()  # enter train mode
    loss_avg = 0.0
    for bx, by in train_loader:
        bx, by = bx.cuda(), by.cuda()

        adv_bx = adversary_train(net, bx, by)

        # forward
        logits = net(adv_bx)

        # backward
        # scheduler.step()
        optimizer.zero_grad()
        loss = F.cross_entropy(logits, by)
        loss.backward()
        optimizer.step()

        # exponential moving average
        loss_avg = loss_avg * 0.8 + float(loss) * 0.2

    state['train_loss'] = loss_avg

# test function
def test():
    net.eval()
    loss_avg = 0.0
    correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.cuda(), target.cuda()

            adv_data = adversary_test(net, data, target)

            # forward
            output = net(adv_data)
            loss = F.cross_entropy(output, target)

            # accuracy
            pred = output.data.max(1)[1]
            correct += pred.eq(target.data).sum().item()

            # test loss average
            loss_avg += float(loss.data)

    state['test_loss'] = loss_avg / len(test_loader)
    state['test_accuracy'] = correct / len(test_loader.dataset)

# overall_test function
def test_in_testset():
    net.eval()
    loss_avg = 0.0
    correct = 0
    adv_loss_avg = 0.0
    adv_correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.cuda(), target.cuda()

            adv_data = adversary_test(net, data, target)

            # forward
            output = net(data)
            loss = F.cross_entropy(output, target)

            # accuracy
            pred = output.data.max(1)[1]
            correct += pred.eq(target.data).sum().item()

            # test loss average
            loss_avg += float(loss.data)

            # forward
            adv_output = net(adv_data)
            adv_loss = F.cross_entropy(adv_output, target)

            # accuracy
            adv_pred = adv_output.data.max(1)[1]
            adv_correct += adv_pred.eq(target.data).sum().item()

            # test loss average
            adv_loss_avg += float(adv_loss.data)

    state['test_loss'] = loss_avg / len(test_loader)
    state['test_accuracy'] = correct / len(test_loader.dataset)
    state['adv_test_loss'] = adv_loss_avg / len(test_loader)
    state['adv_test_accuracy'] = adv_correct / len(test_loader.dataset)

def test_in_trainset():
    train_loader = torch.utils.data.DataLoader(
    train_data, batch_size=opt.test_bs, shuffle=False,
    num_workers=opt.prefetch, pin_memory=torch.cuda.is_available())
    net.eval()
    loss_avg = 0.0
    correct = 0
    adv_loss_avg = 0.0
    adv_correct = 0
    with torch.no_grad():
        for data, target in train_loader:
            data, target = data.cuda(), target.cuda()

            adv_data = adversary_test(net, data, target)

            # forward
            output = net(data)
            loss = F.cross_entropy(output, target)

            # accuracy
            pred = output.data.max(1)[1]
            correct += pred.eq(target.data).sum().item()

            # test loss average
            loss_avg += float(loss.data)

            # forward
            adv_output = net(adv_data)
            adv_loss = F.cross_entropy(adv_output, target)

            # accuracy
            adv_pred = adv_output.data.max(1)[1]
            adv_correct += adv_pred.eq(target.data).sum().item()

            # test loss average
            adv_loss_avg += float(adv_loss.data)

    state['train_loss'] = loss_avg / len(train_loader)
    state['train_accuracy'] = correct / len(train_loader.dataset)
    state['adv_train_loss'] = adv_loss_avg / len(train_loader)
    state['adv_train_accuracy'] = adv_correct / len(train_loader.dataset)

opt = PrivateOptions().parse()

state = {k: v for k, v in opt._get_kwargs()}

torch.manual_seed(opt.random_seed)
np.random.seed(opt.random_seed)
cudnn.benchmark = True

# # mean and standard deviation of channels of CIFAR-10 images
# mean = [x / 255 for x in [125.3, 123.0, 113.9]]
# std = [x / 255 for x in [63.0, 62.1, 66.7]]

train_transform = trn.Compose([trn.RandomHorizontalFlip(), trn.RandomCrop(32, padding=4),
                               trn.ToTensor(), trn.Normalize(
                                         mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])
test_transform = trn.Compose([trn.ToTensor(), trn.Normalize(
                                         mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])

if opt.dataset == 'cifar10':
    train_data = dset.CIFAR10(opt.dataroot, train=True, transform=train_transform, download=True)
    test_data = dset.CIFAR10(opt.dataroot, train=False, transform=test_transform)
    num_classes = 10
else:
    train_data = dset.CIFAR100(opt.dataroot, train=True, transform=train_transform, download=True)
    test_data = dset.CIFAR100(opt.dataroot, train=False, transform=test_transform)
    num_classes = 100

train_loader = torch.utils.data.DataLoader(
    train_data, batch_size=opt.batch_size, shuffle=True,
    num_workers=opt.prefetch, pin_memory=torch.cuda.is_available())
test_loader = torch.utils.data.DataLoader(
    test_data, batch_size=opt.test_bs, shuffle=False,
    num_workers=opt.prefetch, pin_memory=torch.cuda.is_available())

# Create model
if opt.model == 'wrn':
    net = WideResNet(opt.layers, num_classes, opt.widen_factor, dropRate=opt.droprate)
else:
    assert False, opt.model + ' is not supported.'

start_epoch = opt.start_epoch

if opt.ngpu > 0:
    net = torch.nn.DataParallel(net, device_ids=list(range(opt.ngpu)))
    net.cuda()
    torch.cuda.manual_seed(opt.random_seed)

# Restore model if desired
if opt.load != '':
    if opt.test and os.path.isfile(opt.load):
        net.load_state_dict(torch.load(opt.load))
        print('Appointed Model Restored!')
    else:
        model_name = os.path.join(opt.load, opt.dataset + opt.model +
                                  '_epoch_' + str(start_epoch) + '.pt')
        if os.path.isfile(model_name):
            net.load_state_dict(torch.load(model_name))
            print('Model restored! Epoch:', start_epoch)
        else:
            raise Exception("Could not resume")

epoch_step = json.loads(opt.epoch_step)
lr = state['learning_rate']
optimizer = torch.optim.SGD(
    net.parameters(), lr, momentum=state['momentum'],
    weight_decay=state['decay'], nesterov=True)

# def cosine_annealing(step, total_steps, lr_max, lr_min):
#     return lr_min + (lr_max - lr_min) * 0.5 * (
#             1 + np.cos(step / total_steps * np.pi))
#
#
# scheduler = torch.optim.lr_scheduler.LambdaLR(
#     optimizer,
#     lr_lambda=lambda step: cosine_annealing(
#         step,
#         opt.epochs * len(train_loader),
#         1,  # since lr_lambda computes multiplicative factor
#         1e-6 / opt.learning_rate))  # originally 1e-6

adversary_train = pgd.PGD(epsilon=opt.epsilon * 2, num_steps=opt.num_steps, step_size=opt.step_size * 2).cuda()
adversary_test = pgd.PGD(epsilon=opt.epsilon * 2, num_steps=opt.test_num_steps, step_size=opt.test_step_size * 2).cuda()

if opt.test:
    test_in_testset()
    # test_in_trainset()
    print(state)
    exit()

# Make save directory
if not os.path.exists(opt.save):
    os.makedirs(opt.save)
if not os.path.isdir(opt.save):
    raise Exception('%s is not a dir' % opt.save)

with open(os.path.join(opt.save, "log_" + opt.dataset + opt.model +
                                  '_training_results.csv'), 'w') as f:
    f.write('epoch,time(s),train_loss,test_loss,test_accuracy(%)\n')

print('Beginning Training\n')

# Main loop
best_test_accuracy = 0
for epoch in range(start_epoch, opt.epochs + 1):
    state['epoch'] = epoch

    begin_epoch = time.time()

    train()
    test()

    # Save model
    if epoch > 10 and epoch % 10 == 0:
        torch.save(net.state_dict(),
                   os.path.join(opt.save, opt.dataset + opt.model +
                                '_epoch_' + str(epoch) + '.pt'))

    if state['test_accuracy'] > best_test_accuracy:
        best_test_accuracy = state['test_accuracy']
        torch.save(net.state_dict(),
                   os.path.join(opt.save, opt.dataset + opt.model +
                                '_epoch_best.pt'))

    # Show results
    with open(os.path.join(opt.save, "log_" + opt.dataset + opt.model +
                                      '_training_results.csv'), 'a') as f:
        f.write('%03d,%0.6f,%05d,%0.3f,%0.3f,%0.2f\n' % (
            (epoch),
            lr,
            time.time() - begin_epoch,
            state['train_loss'],
            state['test_loss'],
            100. * state['test_accuracy'],
        ))

    print('Epoch {0:3d} | LR {1:.6f} | Time {2:5d} | Train Loss {3:.3f} | Test Loss {4:.3f} | Test Acc {5:.2f}'.format(
        (epoch),
        lr,
        int(time.time() - begin_epoch),
        state['train_loss'],
        state['test_loss'],
        100. * state['test_accuracy'])
    )

    # Adjust learning rate
    if epoch in epoch_step:
        lr = optimizer.param_groups[0]['lr'] * opt.lr_decay_ratio
        optimizer = torch.optim.SGD(
            net.parameters(), lr, momentum=state['momentum'],
            weight_decay=state['decay'], nesterov=True)
        print("new lr:", lr)


nums = [1,2,3,4,5,6,7,8,9,10]

#Print the even numbers
even_numbers = list(filter(lambda x: x % 2 == 0, nums))
print(even_numbers)

#Print the odd numbers
odd_numbers = list(filter(lambda x: x % 2 != 0, nums))
print(odd_numbers)

names = ['Adam', 'Ana', 'Kevin', 'Daniel', 'Michael']

#Filter the names that have more than 5 characters od length

filtered_names = list(filter(lambda name: len(name) > 5, names))

print(filtered_names)


# -*- coding: utf-8 -*-

from django.conf.urls import patterns, include, url

urlpatterns = patterns('CiscoDxUketsukeApp.views',
#     url(r'^$', 'CiscoDxUketsuke.views.home', name='home'),
#     url(r'^getData/' 'CiscoDxUketsuke.views.getData'),
    url(r'^member_tsv/$','member_tsv'),
    url(r'^member_json/$','member_json'),
    url(r'^room_tsv/$','room_tsv'),
    url(r'^room_json/$','room_json'),
    url(r'^folder_tsv/$','folder_tsv'),
    url(r'^folder_json/$','folder_json'),
    url(r'^favorite_tsv/$','favorite_tsv'),
    url(r'^favorite_json/$','favorite_json'),
    url(r'^test/$','test'),
    url(r'^test2/$','test2'),
    url(r'^pad1/$','pad1'),
    url(r'^pad2/$','pad2'),
    url(r'^top/$','top'),
    url(r'^member/$','member'),
    url(r'^room/$','room'),
    url(r'^folder/$','folder'),
    url(r'^folder/(?P<dxId>\d+)/$','folder'),
    url(r'^folder/(?P<dxId>\d+)/(?P<folderId>\d+)/$','folder'),
    url(r'^home/$','home'),
    url(r'^fav/$','fav'),
    url(r'^index/$','index'),
    url(r'^list/$','list'),
    url(r'^add_dx/$','add_dx'),
    url(r'^edit_dx/$','edit_dx'),
    url(r'^add_member/$','add_member'),
    url(r'^add_room/$','add_room'),
    url(r'^add_folder/$','add_folder'),
    url(r'^add1/$','add_member_room_db'),
)


import cv2
# Problem 4.
# Rescale the video vid1.jpg by 0.5 and display the original video and the rescaled one in separate windows.

def rescaleFrame(frame, scale):
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)
    dimensions = (width, height)

    return cv2.resize(frame, dimensions, interpolation=cv2.INTER_AREA)

capture = cv2.VideoCapture('vid1.mp4')

while True:
    frame_loaded, frame = capture.read()

    if frame is not None:  # or if isTrue
        frame_rescaled = rescaleFrame(frame, 0.5)
        cv2.imshow('Video', frame)
        cv2.imshow('Video_rescaled', frame_rescaled)
    else:
        print('empty frame')
        exit(1)

    if cv2.waitKey(20) & 0xFF == ord('d'):
        break

capture.release()
cv2.destroyAllWindows()
cv2.waitKey(0)


from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.remote.webelement import WebElement
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
import time
import json
import os
from course import Course

# Web Driver configuration
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
coursesList = []

# getting pages URLs
f = open("URL.txt", "r")
URLs = []
for x in f:
    URLs.append(x)
f.close()

# searching through each page from file and through each subpage (< 1 2 3 ... 7 >)
for URL in URLs:
    emptyPage = False # means that the page number is out of range and there is no more content on this page
    subpageCounter = 1
    while not emptyPage:
        print(URL+'&p='+str(subpageCounter))
        driver.get(URL+'&p='+str(subpageCounter))
        subpageCounter += 1
        try: # element with this class name is a big container for all smaller divs. If it is not present then there is no content on the page
            WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'course-list--container--3zXPS')))
            container = driver.find_element_by_class_name('course-list--container--3zXPS')
            coursesBiggerDivs = container.find_elements_by_class_name('browse-course-card--link--3KIkQ')
            courses = container.find_elements_by_class_name('course-card--container--3w8Zm')
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            counter = 0
            for course in courses: # each course we convert into an object of 'Course' class (data extraction)
                title = course.find_element_by_class_name('udlite-heading-md').text
                desc = course.find_element_by_class_name('udlite-text-sm').text
                author = course.find_element_by_class_name('udlite-text-xs').text

                try:
                    spanElement = course.find_element_by_css_selector('span.star-rating--rating-number--3lVe8')
                except NoSuchElementException:
                    ratings = 'Brak ocen'
                else:
                    ratings = spanElement.text

                try:
                    details = course.find_elements_by_css_selector('span.course-card--row--1OMjg')
                    courseLength = details[0].text
                    courseLevel = details[len(details)-1].text
                except NoSuchElementException:
                    print("Brak dodatkowych informacji")
                    courseLength = 'Brak informacji'
                    courseLevel = 'Brak informacji'

                try:
                    image = course.find_element_by_class_name('course-card--course-image--2sjYP')
                    ActionChains(driver).move_to_element(image).perform()
                    imageSourceURL = image.get_attribute('src')
                except NoSuchElementException:
                    print("Brak zdjęcia")
                    imageSourceURL = 'https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.smarthome.com.au%2Faeotec-z-wave-plug-in-smart-switch-6.html&psig=AOvVaw33Vx1wP6a3B3QAn_6WPe4A&ust=1602514347326000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCNitsanlrOwCFQAAAAAdAAAAABAE'

                try:
                    priceDiv = course.find_element_by_css_selector('div.price-text--price-part--Tu6MH')
                    ActionChains(driver).move_to_element(priceDiv).perform()
                    spans = priceDiv.find_elements_by_tag_name('span')
                    price = spans[len(spans) - 1].text
                except NoSuchElementException:
                    price = 'Brak ceny'

                try:
                    courseLink = coursesBiggerDivs[counter].get_attribute('href')
                except NoSuchElementException:
                    courseLink = None

                counter += 1
                c = Course(title, desc, author, ratings, price, imageSourceURL, courseLength, courseLevel, courseLink)
                coursesList.append(c)

        except TimeoutException:
            print('[INFO] Ostatnia podstrona adresu URL')
            emptyPage = True

os.remove('objectsInJSON.txt')

for course in coursesList: #search through each course page and get some more specific information
    driver.get(course.URL)
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'topic-menu')))
    topicDiv = driver.find_element_by_class_name('topic-menu')
    elements = topicDiv.find_elements_by_class_name('udlite-heading-sm')
    course.setCategory(elements[0].text)
    course.setSubcategory(elements[1].text)
    courseDescription = driver.find_element_by_class_name('styles--description--3y4KY')
    course.setExtendedDescription(courseDescription.get_attribute('innerHTML'))

    # write converted course object into output file
    string = course.makeJSON()
    with open('objectsInJSON.txt','a',encoding='utf-8') as file:
        json.dump(string, file, ensure_ascii=False)
        file.write("\n")

driver.quit()
file.close()


import io
import numpy as np
import sys
from gym.envs.toy_test import discrete
from copy import deepcopy as dc

UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3

class GridworldEnv(discrete.DiscreteEnv):

    metadata = {'render.modes': ['human', 'ansi']}

    def __init__(self, shape = [4, 4]):
        if not isinstance(shape, (list, tuple)) or not len(shape) == 2:
            raise ValueError('shape argument must be a list/tuple of length 2')

        self.shape = shape

        nS = np.prod(shape) #np.prod : array 내부 element들의 곱
        nA = 4

        MAX_Y = shape[0]
        MAX_X = shape[1]

        P = {}
        grid = np.arange(nS).reshape(shape) # np.arange(x) 0부터 x까지 [0, 1, ..., x]
        it = np.nditer(grid, flags=['multi_index']) # iterator

        while not it.finished:
            s = it.iterindex
            y, x = it.multi_index # 왜 y,x 순서? => (row, column)가 (y, x) 에 대응

            P[s] = {a: [] for a in range(nA)}  # a = 0, ..,3 돌면서 [] 생성 (s = iterindex 이고 state)
            # P[s][a] = (prob, next_state, reward, is_done)

            def is_done(s): # terminal or not
                return s == 0 or s == (nS - 1)
            reward = 0.0 if is_done(s) else -1.0 # reward는 현재 state와 action 기준 (여기서는 action 종류 관계없이 동일)

            if is_done(s):
                P[s][UP] = [(1.0, s, reward, True)] # 왜 [ ]?
                P[s][RIGHT] = [(1.0, s, reward, True)]
                P[s][DOWN] = [(1.0, s, reward, True)]
                P[s][LEFT] = [(1.0, s, reward, True)]
            else:
                ns_up = s if y == 0 else s - MAX_X # 맨 윗줄이면 그대로, 아니면 MAX_X 만큼 빼기 (s는 1차원 배열이니까)
                ns_right = s if x == (MAX_X - 1) else s + 1
                ns_down = s if y == (MAX_Y -1) else s + MAX_X
                ns_left = s if x == 0 else s - 1
                P[s][UP] = [(1.0, ns_up, reward, is_done(ns_up))]
                P[s][RIGHT] = [(1.0, ns_right, reward, is_done(ns_right))]
                P[s][DOWN] = [(1.0, ns_down, reward, is_done(ns_down))]
                P[s][LEFT] = [(1.0, ns_left, reward, is_done(ns_left))]

            it.iternext()


# pylint: disable=no-member, no-name-in-module, import-error

from __future__ import absolute_import
import glob
import distutils.command.sdist
import distutils.log
import subprocess
from setuptools import Command, setup
import setuptools.command.sdist

# Patch setuptools' sdist behaviour with distutils' sdist behaviour
setuptools.command.sdist.sdist.run = distutils.command.sdist.sdist.run

class LintCommand(Command):
    """
    Custom setuptools command for running lint
    """
    description = 'run lint against project source files'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        self.announce("Running pylint", level=distutils.log.INFO)
        subprocess.check_call(["pylint"] + glob.glob("*.py"))

setup(
    # Package name:
    name="dxlmisc",

    # Version number:
    version="0.0.1",

    # Requirements
    install_requires=[
        "pylint"
    ],

    description="Misc OpenDXL Tools",

    python_requires='>=2.7.9,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*',

    classifiers=[
        "Development Status :: 4 - Beta",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "License :: OSI Approved :: Apache Software License",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 2.7",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.4",
        "Programming Language :: Python :: 3.5",
        "Programming Language :: Python :: 3.6"
    ],

    cmdclass={
        "lint": LintCommand
    }
)


from typing import List

class Solution:
    def deleteAndEarn(self, nums: List[int]) -> int:
        max_val = max(nums)

        total = [0] * (max_val + 1)
        for val in nums:
            total[val] += val

        def rob(nums):
            first = nums[0]
            second = max(nums[0], nums[1])
            for i in range(2, len(nums)):
                first, second = second, max(second, first + nums[i])

            return second

        return rob(total)

740. 删除并获得点数.py


import string
import os
import csv
from datetime import datetime
from datetime import date
import re

#debugging
debug = False
print()

def extract(inputFile: string, outputFile: string):
    specialTitle = False
    fileIndex = 1

    with open(outputFile,'w',newline='', encoding="utf8") as writefile,open(inputFile,newline='', encoding="utf8") as readfile:
        writer = csv.writer(writefile)

        state = 0
        processNum = 0
        starti = 0
        for line in readfile:

        #PRELIMINARY----------------------------------------------------------
            states = ['',                                               #0  just read line
                    '[MARS]',                                           #1
                    '[CONFIG LIST]',                                    #2
                    'Process usage summary',                            #3
                    '[Batterystats Collector]',                         #4
                    'Daily Summary',                                    #5
                    '[PowerAnomaly Battery Dump]',                      #6
                    'Anomaly List For DeviceCare',                      #7
                    '[TCPU dump]',                                      #8
                    '[UCPU dump]',                                      #9
                    '------ NETWORK DEV INFO (/proc/net/dev) ------',   #10
                    '------ MEMORY INFO (/proc/meminfo) ------',        #11
                    '------ VIRTUAL MEMORY STATS (/proc/vmstat) ------',#12
                    '** MEMINFO in pid[com.x3AM.Innovations.Flare.Mobile.App] **'#13

                    ]

            data = ['']
        #/////////////////////////////////////////////////////////////////////

        #SET STATE------------------------------------------------------------
            #enter state 1 ([MARS])
            if (line.strip() == states[1]):
                state = 1
                starti = fileIndex
                print(f"--Changed State to: {state} becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 2 ([CONFIG LIST])
            elif (line.strip() == states[2]):
                state = 2
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerows([[states[state]], ['Boot Number', 'Date/Time']])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 3 (Process usage summary)
            elif (line.strip()[:21] == states[3]):        #[:21] take the first 21 characters
                state = 3
                processNum = 0
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 4 ([Batterystats Collector])
            elif (line.strip() == states[4]):
                state = 4
                processNum = 0
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 5 (Daily Summary)
            elif (line.strip() == states[5]):
                state = 5
                processNum = 0
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([])
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 6 ([PowerAnomaly Battery Dump])
            elif (line.strip() == states[6]):
                state = 6
                processNum = 0
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 7 (Anomaly List For DeviceCare)
            elif (line.strip() == states[7]):
                state = 7
                processNum = 0
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([])
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 8 ([TCPU dump])
            elif (line.strip() == states[8]):
                state = 8
                processNum = 0
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([])
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 9 ([UCPU dump])
            elif (line.strip() == states[9]):
                state = 9
                processNum = 0
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([])
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 10 (------ NETWORK DEV INFO (/proc/net/dev) ------ )
            elif (line.strip() == states[10]):
                state = 10
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 11 (------ MEMORY INFO (/proc/meminfo) ------ )
            elif(line.strip() == states[11]):
                state = 11
                processNum = 0
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            #enter state 12 (------ VIRTUAL MEMORY STATS (/proc/vmstat) ------)
            elif(line.strip() == states[12]):
                state = 12
                processNum = 0
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")

            '''
            #enter state 13 (** MEMINFO in pid[com.x3AM.Innovations.Flare.Mobile.App] **)
            elif(line.strip()[:17] == states[13][:17]  and  re.search('[com.x3AM.Innovations.Flare.Mobile.App]', line)==True):
                state = 13
                processNum = 0
                starti = fileIndex
                #handle special title
                specialTitle = True
                writer.writerow([line])
                print(f"--Changed State to: {state} w/ special title becasuse of {states[state]}\nExtracting from line {starti}...")
            '''

            #debug info
            #print(f"fileIndex: {fileIndex} | i: {i} | state: {state}")

        #/////////////////////////////////////////////////////////////////////

        #PERFORM STATE ACTIONS----------------------------------------------------
            #extract MARS                               (state 1)
            if(state == 1):
                #when done, set state to inactive
                if(line.strip() == ''):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                data = line.split()

            #extract CONFIG LIST                        (state 2)
            if(state == 2):
                #when done, set state to inactive
                if(line.strip() == ''):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([''])

                #close special title
                if(fileIndex!=starti  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                if(not specialTitle and state!=0):
                    raw = line.split()
                    data[0] = (raw[0])
                    data.append(f"{raw[1]} {raw[2]} ")

            #extract power usage summary                (state 3)
            if(state == 3):
                #finish at end flag
                if(line.strip() == 'SSRM MEMORY DUMP **********'):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                #close special title
                if(fileIndex!=starti  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                #take usage title
                if(processNum!=1  and  line[0] == '['  and  state!=0):
                    processNum = 1

                #collect data for usage title
                if(processNum == 1 and state!=0):
                    data = line.split('|')
                    for elem in data:
                        elem = elem.strip()

                #mark new usage title
                if(line.strip() == '' and state!=0):
                    processNum = 0

            #extract Batterystats                       (state 4)
            if(state == 4):
                trimmed = line.strip()
                #finish will finish when state is changed at 'set state' level

                #close special title
                if(fileIndex!=starti  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                #mark new usage title
                if(line.strip() == '' and state==4):
                    processNum = 0

                #set process
                if(trimmed != ''  and  line[0] == 'S'  and  state==4):
                    processNum = 3
                if(trimmed != ''  and  line[2] == 'U'  and  state==4):
                    processNum = 2
                if(trimmed != ''  and  processNum == 2  and  line[2] == ' '):
                    processNum = 2
                if(trimmed != ''  and  processNum != 2  and  line[0] == ' '  and  state==4):
                    processNum = 1

                #collect prelim data for usage (process 1)
                if(processNum == 1 and state==4):
                    data = line.split(":")
                    for elem in data:
                        elem = elem.strip()

                #collect data for usage title (process 2)
                if(processNum == 2 and state==4):
                    data = line.split('|')
                    for elem in data:
                        elem = elem.strip()

                #collect title
                if(processNum == 3  and  state==4):
                    raw = line.split()
                    data[0] = f"{raw[2]} {raw[3]}"
                    data.append(f"{raw[5]} {raw[6]}")

            #extract Daily Summary                      (state 5)
            if(state == 5):
                #finish at end flag
                if(line.strip() == ''):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                #close special title
                if(fileIndex>starti+1  and  specialTitle  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                #parse data
                data = line.split('|')
                for elem in data:
                    elem = elem.strip()

            #extract PowerAnomaly Battery Dump          (state 6)
            if(state == 6):
                #close special title
                if(fileIndex>starti  and  specialTitle  and  state == 6):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                #finish at next title
                if(line[:3] == '[Ba'):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                if(state == 6):
                    data = line.strip()

            #extract Anomaly List For DeviceCare        (state 7)
            if(state == 7):
                #finish parsing after second null line
                if(line.strip() == ''  and  processNum == 2):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                #next process after null line
                if(line.strip() == ''  and  state == 7):
                    processNum = 1
                    print(f"Process Number has been changed to {processNum} at line {fileIndex}")

                #close special title
                if(fileIndex>starti  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                #parse data by default
                if(processNum == 0  and  state == 7):
                    data = line.split('|')
                    newData = []
                    for elem in data:
                        elem = elem.strip()
                        newData.append(elem)
                    data = newData

                #collect stats
                elif(processNum == 2  and  state == 7):
                    data = line.split(':')
                    #print(f"{data[0]} : {data[1]}")

                #collect stats timestamp
                elif(line.strip() != ''  and  processNum == 1):
                    raw = line.split()
                    data[0] = f"{raw[2]} {raw[3]}"
                    data.append(f"{raw[5]} {raw[6]}")
                    processNum = 2
                    print(f"Process Number has been changed to {processNum} at line {fileIndex}")

            #extract [TCPU dump]                        (state 8)
            if(state == 8):
                #finish at end flag
                if(line.strip() == ''):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                #close special title
                if(fileIndex>starti+1  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                #parse data
                data = line.split('|')
                for elem in data:
                    elem = elem.strip()

            #extract [UCPU dump]                        (state 9)
            if(state == 9):
                #finish at end flag
                if(line.strip() == ''):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                #close special title
                if(fileIndex>starti+1  and  specialTitle  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                #parse data
                data = line.split('|')
                for elem in data:
                    elem = elem.strip()

            #extract NETWORK DEV INFO                   (state 10)
            if(state == 10):
                #when done, set state to inactive
                if(line.strip()!=''  and  line.split()[0] == '------'  and  not specialTitle):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                #close special title
                if(fileIndex>starti+1  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                #collect data
                if(state == 10  and  not specialTitle):
                    line = line.strip()
                    data = line.split()

                    keep = False
                    for elem in data:
                        if(elem.isnumeric()  and  int(elem)>10000):
                                keep = True

                    if(not keep):
                        data = []

            #extract MEMORY INFO                        (state 11)
            if(state == 11):
                #when done, set state to inactive
                if(line.strip()!=''  and  line.split()[0] == '------'  and  not specialTitle):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                #close special title
                if(fileIndex>starti+1  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                if(line.strip()!=''  and  not specialTitle and state==11):
                    raw = line.split(':')
                    data[0] = raw[0].strip()
                    data.append(raw[1].strip())

            #extract VIRTUAL MEMORY STATS               (state 12)
            if(state == 12):
                #when done, set state to inactive
                if(line.strip()!=''  and  line.split()[0] == '------'  and  not specialTitle):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                #close special title
                if(fileIndex>starti  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                if(line.strip()!=''  and  not specialTitle and state==11):
                    raw = line.split()
                    data[0] = raw[0].strip()
                    data.append(raw[1].strip())

            '''
            #extract MEMINFO in pid                     (state 13)
            if(state == 13):
                #when done, set state to inactive
                if(line.strip()!=''  and  line.split()[0] == '*'  and  not specialTitle):
                    state = 0
                    print(f"--Reverted State to {state} at {fileIndex}--\n")
                    writer.writerow([])

                #close special title
                if(fileIndex>starti  and  specialTitle):
                    specialTitle = False
                    if(debug):
                        print(f"Ended specialTitle at line {fileIndex}")

                #collect data
                if(processNum == 2  and  line.strip()!=''  and  not specialTitle  and  state==13):
                    data = line.split()
                    for elem in data:
                        elem = elem.strip()

                #collect title
                if(processNum == 1):
                    data[0] = line
                    processNum = 2
                if(line.strip()==''  and  not specialTitle  and  state==13):
                    processNum = 1
            '''

        #/////////////////////////////////////////////////////////////////////

            #output if the state is active (not 0)
            if(state!=0 and (not specialTitle)):
                    #print(f"writing {data}")
                    isEmpty = True
                    for elem in data:
                        if(elem.strip() != ''):
                            isEmpty = False
                            break
                    if(not isEmpty):
                        writer.writerow(data)
                    elif(debug):
                        print(f"Did not write line {fileIndex} to CSV because data was empty")
            if(specialTitle  and  debug):
                print(f"Did not write line {fileIndex} to CSV because of Special Title")

            fileIndex += 1

#   main
inputFiles = os.listdir('INPUT')
fileCount = 1
for file in inputFiles:
    outfile = f"{file.split('.')[0]}-output_{fileCount}.csv"
    print(f'RUNNING EXTRACT ON [{file}] AND WRITING TO [{outfile}]...')
    print()
    extract(f'INPUT\{file}', outfile)
    print("Exited properly\n")
    fileCount+=1


# Imports
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
import users
from datetime import datetime

"""
Модуль для поиска атлетов по парамтрам пользователя
"""
# Variables
Base = declarative_base()

# Class definitions

class Athlette(Base):
    __tablename__ = "Athelete"
    id = sa.Column(sa.INTEGER, primary_key=True)
    age = sa.Column(sa.INTEGER)
    birthdate = sa.Column(sa.TEXT)
    gender = sa.Column(sa.TEXT)
    height = sa.Column(sa.REAL)
    name = sa.Column(sa.TEXT)
    weight = sa.Column(sa.INTEGER)
    gold_medals = sa.Column(sa.INTEGER)
    silver_medals = sa.Column(sa.INTEGER)
    bronze_medals = sa.Column(sa.INTEGER)
    total_medals = sa.Column(sa.INTEGER)
    sport = sa.Column(sa.TEXT)
    country = sa.Column(sa.TEXT)

# Function definitions
def search_id(id, session):
    query_str = session.query(Athlette).filter(Athlette.id == id).first()
    usr = f"{query_str}"
    return usr

def height_compare(id, session, bcolors):
    """
    Сравнение роста атлетов с пользовательским
    """
    # берем из базы рост пользователя
    usr_query = session.query(users.User).filter(users.User.id == id).first()
    usr_height = usr_query.height

    # ищем атлетов по росту пользователя
    ath_query = session.query(Athlette).filter(
        Athlette.height == usr_height)

    ath_count = ath_query.count()
    ath_found = ath_query.all()

    # выводим содержимое объектов Athlete, если найдены
    res = ""
    if ath_found:
        for ath in ath_found:
            res += f"  {ath.name}, {ath.sport} \n"
        res = f"{res}\n  Всего атлетов с ростом {ath.height} метра: {ath_count}"
    else:
        print(bcolors.FAIL +
              f"\nERROR: Атлет с ростом {usr_height}m не найден" + bcolors.ENDC)

    return res

def bday_compare(id, session):
    """
    Ищем атлета, наиболее близкого по дате рождения к пользователю
    """
    dt_format = '%Y-%m-%d'
    usr_query = session.query(users.User).filter(users.User.id == id).first()
    user_bday_str = usr_query.birthdate
    user_bday_dt_obj = datetime.strptime(user_bday_str, dt_format)

    ath_query_all_obj = session.query(Athlette).all()

    ath_bday_all_dt_list = list()
    for ath in ath_query_all_obj:
        ath_bday_all_dt_list.append(
            datetime.strptime(ath.birthdate, dt_format))

    closest_bday_dt_obj = ath_bday_all_dt_list[min(range(len(ath_bday_all_dt_list)),
                                                   key=lambda i: abs(ath_bday_all_dt_list[i]-user_bday_dt_obj))]

    # выбираем всех атлетов по самой ближней дате рождения
    closest_bday_str = closest_bday_dt_obj.strftime(dt_format)
    ath_query_bday_query = session.query(Athlette).filter(
        Athlette.birthdate == closest_bday_str)

    # берем из базы данные и считаем
    ath_bday_obj = ath_query_bday_query.all()
    ath_bday_count = ath_query_bday_query.count()

    # формируем возврат
    res = ""
    for ath in ath_bday_obj:
        res = f"{res}\n  {ath.name}, д.р.: {ath.birthdate}, {ath.sport}"

    return res

if __name__ == "__main__":
    print("ERROR: Запуск скрипта через выполнение модуля start.py \n")

# DEBUG
# print('Info: Module find_athlete.py - imported')


from flask import Blueprint, render_template, url_for

lonely = Blueprint('lonely',
                   __name__,
                   template_folder='./',
                   static_folder='./',
                   static_url_path='/')
lonely.display_name = 'Lonely'
lonely.published = True
lonely.description = "An interactive visualization of original music."

@lonely.route('/')
def _lonely():
    return render_template('lonely.html')


from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import os

try:
    link = "http://suninjuly.github.io/file_input.html"
    browser = webdriver.Chrome()
    browser.get(link)

    elements = browser.find_elements(By.CSS_SELECTOR, ".form-control")
    for element in elements:
        if element.get_attribute('required') != None:
            element.send_keys("Мой ответ")

    print(os.getcwd())
    current_dir = os.path.realpath(os.path.dirname(__file__))
    file_path = os.path.join(current_dir, 'file.txt')
    print(file_path)
    element = browser.find_element(By.CSS_SELECTOR, "#file")
    element.send_keys(file_path)

    button = browser.find_element(By.CSS_SELECTOR, "button.btn")
    button.click()

finally:
    # ожидание чтобы визуально оценить результаты прохождения скрипта
    time.sleep(10)
    # закрываем браузер после всех манипуляций
    browser.quit()


def convert_ascii(letter):
    result = ord(letter)
    return result

def convert_binary(num):
    result = bin(num)
    return result

def menu():
    print("=============\nMenu\n=============\n 1. Character\n 2. Word")
    option = int(input("Please select an option to convert into binary: "))

    if option == 1:
        chosenLetter = input("Write the letter: ")
        ascii_num = convert_ascii(chosenLetter)
        print("=============\nResults\n=============\n")
        print(
            "ASCII character value of", chosenLetter, "is", ascii_num ,
            "and representation of", chosenLetter, "in a byte is", convert_binary(ascii_num)
            )

    elif option == 2:
        chosenWord = input("Write the word: ")
        binaryWord = []
        print("=============\nResults\n=============\n")
        for i in chosenWord:
            ascii_num = convert_ascii(i)
            convertion = convert_binary(ascii_num)
            print(
                "ASCII character value of", i, "is", ascii_num ,
                "and representation of", i, "in a byte is", convertion
                )
            binaryWord.append(convertion)
        print(" ".join(binaryWord))

    else:
        exit()

menu()


import re
import os
import sys
import nltk
import json
import wandb
import joblib
import datasets
import numpy as np
import pandas as pd
from time import process_time
from nltk import word_tokenize
from nltk.stem import WordNetLemmatizer
from sklearn.svm import LinearSVC
from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import TfidfVectorizer

np.random.seed(42)

class LemmaTokenizer:
        ignore_tokens = [',', '.', ';', ':', '"', '``', "''", '`']
        def __init__(self):
            self.wnl = WordNetLemmatizer()
        def __call__(self, doc):
            return [self.wnl.lemmatize(t) for t in word_tokenize(doc) if t not in self.ignore_tokens]

def prepare_dataset(data_folder, label2id, data_types, max_length):
    def combine_data(example):
        temp_text = ""
        for data_type in data_types:
            temp_text += example[data_type] + " "
        example["text"] = temp_text[:max_length]
        return example

    dataset = datasets.load_from_disk(data_folder + "dataset/")
    dataset = dataset["train"]
    dataset_encoded = dataset.class_encode_column("category")
    dataset_aligned = dataset_encoded.align_labels_with_mapping(label2id, "category")
    dataset_cleaned = dataset_aligned.map(combine_data)

    dataset = dataset_cleaned.remove_columns(["title", "body"])
    dataset = dataset.rename_column("category", "label")
    return dataset

def main():
    hps = {
        "data_types": ["title", "body"],
        "loss_function": "squared_hinge",
        "ngram_range": 3,
        "max_length": 512,
    }

    wandb_id = wandb.util.generate_id()

    run = wandb.init(
        project="DMOZ-classification",
        config=hps,
        job_type="training",
        name="SVM_DMOZ_" + str(wandb_id),
        tags=["SVM", "DMOZ"],
    )

    data_folder = "/ceph/csedu-scratch/other/jbrons/thesis-web-classification/"

    id2label = {0: "Arts", 1: "Business", 2: "Computers", 3: "Health", 4: "Home", 5: "News", 6: "Recreation", 7: "Reference", 8: "Science", 9: "Shopping", 10: "Society", 11: "Sports", 12: "Games"}
    label2id = {v: k for k, v in id2label.items()}
    labels = label2id.keys()

    dataset = prepare_dataset(data_folder, label2id, hps["data_types"], hps["max_length"])
    X_train, y_train = dataset["text"], dataset["label"]

    tokenizer=LemmaTokenizer()

    pipeline = make_pipeline(
        TfidfVectorizer(
            ngram_range=(1, hps["ngram_range"]),
            tokenizer=tokenizer,
            token_pattern=None
        ),
        LinearSVC(loss=hps["loss_function"])
    )

    t0 = process_time()
    pipeline.fit(X_train, y_train)
    training_time = process_time() - t0

    print("Training time {:5.2f}s for {:0d} samples.".format(training_time, len(y_train)))
    run.summary["training_time"] = training_time

    filename = data_folder + "models/SVM/model.pkl"
    joblib.dump(pipeline, filename, compress=3)

    model_artifact = wandb.Artifact(
        name="model_SVM_DMOZ",
        type="model"
    )
    model_artifact.add_file(thesis_folder + "models/SVM/model.pkl")
    run.log_artifact(model_artifact)

if __name__ == "__main__":
    main()


from django.shortcuts import render, redirect
from django.contrib import messages
from .models import User
from .forms import RegisterForm, LoginForm
from .utils import require_login

def login_page(request):
	context = {"reg_form": RegisterForm(), "login_form": LoginForm()}
	return render(request, "users/login.html", context)

def login(request):
	curr_user = User.user_manager.login(request.POST["email_address"], request.POST["password"])
	if not curr_user:
		messages.error(request, "E-mail or password incorrect")
		return redirect("login_page")
	else:
		request.session["curr_user"] = curr_user.id
		return redirect("dashboard")

def register(request):
	registered, guy_or_errors = User.user_manager.register(request.POST)

	if not registered:
		for error in guy_or_errors: messages.error(request, error)
		return redirect("login_page")
	else:
		request.session["curr_user"] = guy_or_errors.id
		return redirect("dashboard")

def log_off(request):
	request.session.clear()
	return redirect("login_page")

@require_login
def dashboard(request, curr_user):
	context = {
		"curr_user": curr_user,
		"users": User.user_manager.all(),
	}
	return render(request, "users/dashboard.html", context)

@require_login
def show(request, curr_user, id):
	print("show page")
	context = {
		"curr_user": curr_user,
		"user": User.user_manager.get(id=id),
	}

	return render(request, "users/show.html", context)

@require_login
def edit(request, curr_user, id):
	context = {
		"curr_user": curr_user,
		"user": User.user_manager.get(id=id),
	}
	return render(request, "users/edit.html", context)

@require_login
def update(request, curr_user, id):
	# Logic to check if curr_user is admin or user being updated
	if not (curr_user.admin or curr_user.id == int(id)):
		print(curr_user.id, id, curr_user.id == id)
		return redirect("/dashboard")

	# Logic to actually update
	errors = User.user_manager.update(id, request.POST)

	if errors:
		for error in errors:
			messages.error(request, error)
		return redirect("edit", id=id)
	else:
		return redirect("show", id=id)


#!/usr/bin/python3
import subprocess
import json
import requests
import time
import logging
import os

#bin Paths
ipfspath = '/usr/local/bin/ipfs'
wgetpath = '/usr/bin/wget'
wcpath = '/usr/bin/wc'

#Basic logging to ipfspodcastnode.log
logging.basicConfig(format="%(asctime)s : %(message)s", datefmt="%Y-%m-%d %H:%M:%S", filename="ipfspodcastnode.log", filemode="w", level=logging.INFO)

#Create an empty email.cfg (if it doesn't exist)
if not os.path.exists('cfg/email.cfg'):
  with open('cfg/email.cfg', 'w') as ecf:
    ecf.write('')

#Init IPFS (if necessary)
if not os.path.exists('ipfs/config'):
  logging.info('Initializing IPFS')
  ipfs_init = subprocess.run(ipfspath + ' init', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

#Start WebUI
import webui
logging.info('Starting Web UI')

#Automatically discover relays and advertise relay addresses when behind NAT.
swarmnat = subprocess.run(ipfspath + ' config --json Swarm.RelayClient.Enabled true', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

#Start IPFS
daemon = subprocess.run(ipfspath + ' daemon >/dev/null 2>&1 &', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
logging.info('Starting IPFS Daemon')
time.sleep(10)

#Get IPFS ID
with open('ipfs/config', 'r') as ipcfg:
  ipconfig = ipcfg.read()
  jtxt = json.loads(ipconfig)
  logging.info('IPFS ID : ' + jtxt['Identity']['PeerID'])

#Main loop
while True:

  #Request payload
  payload = { 'version': 0.6, 'ipfs_id': jtxt['Identity']['PeerID'] }

  #Read E-mail Config
  with open('cfg/email.cfg', 'r') as ecf:
    email = ecf.read()
    if email == '':
      email = 'user@example.com'
  payload['email'] = email

  #Check if IPFS is running, restart if necessary.
  payload['online'] = False
  diag = subprocess.run(ipfspath + ' diag sys', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  if diag.returncode == 0:
    ipfs = json.loads(diag.stdout)
    payload['ipfs_ver'] = ipfs['ipfs_version']
    payload['online'] = ipfs['net']['online']
  if payload['online'] == False:
    #Start the IPFS daemon
    daemon = subprocess.run(ipfspath + ' daemon >/dev/null 2>&1 &', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    logging.info('@@@ IPFS NOT RUNNING !!! Restarting Daemon @@@')

  #Get Peer Count
  peercnt = 0
  speers = subprocess.run(ipfspath + ' swarm peers|wc -l', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  if speers.returncode == 0:
    peercnt = speers.stdout.decode().strip()
  payload['peers'] = peercnt

  #Request work
  logging.info('Requesting Work...')
  try:
    response = requests.post("https://IPFSPodcasting.net/Request", timeout=120, data=payload)
    work = json.loads(response.text)
    logging.info('Response : ' + str(work))
  except requests.RequestException as e:
    logging.info('Error during request : ' + str(e))
    work = { 'message': 'Request Error' }

  if work['message'] == 'Request Error':
    logging.info('Error requesting work from IPFSPodcasting.net (check internet / firewall / router).')

  elif work['message'][0:7] != 'No Work':
    if work['download'] != '' and work['filename'] != '':
      logging.info('Downloading ' + str(work['download']))
      #Download any "downloads" and Add to IPFS (1hr48min timeout)
      try:
        hash = subprocess.run(wgetpath + ' -q --no-check-certificate "' + work['download'] + '" -O - | ' + ipfspath + ' add -q -w --stdin-name "' + work['filename'] + '"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=6500)
        hashcode = hash.returncode
      except subprocess.SubprocessError as e:
        logging.info('Error downloading/pinning episode : ' + str(e))
        hashcode = 99

      if hashcode == 0:
        #Get file size (for validation)
        downhash=hash.stdout.decode().strip().split('\n')
        size = subprocess.run(ipfspath + ' cat ' + downhash[0] + ' | ' + wcpath + ' -c', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        downsize=size.stdout.decode().strip()
        logging.info('Added to IPFS ( hash : ' + str(downhash[0]) + ' length : ' + str(downsize) + ')')
        payload['downloaded'] = downhash[0] + '/' + downhash[1]
        payload['length'] = downsize
      else:
        payload['error'] = hashcode

    if work['pin'] != '':
      #Directly pin if already in IPFS
      logging.info('Pinning hash (' + str(work['pin']) + ')')
      try:
        pin = subprocess.run(ipfspath + ' pin add ' + work['pin'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=6500)
        pincode = pin.returncode
      except subprocess.SubprocessError as e:
        logging.info('Error direct pinning : ' + str(e))
        #Clean up any other pin commands that may have spawned
        cleanup = subprocess.run('kill `ps aux|grep "ipfs pin ad[d]"|awk \'{ print $2 }\'`', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        pincode = 98

      if pincode == 0:
        #Verify Success and return full CID & Length
        pinchk = subprocess.run(ipfspath + ' ls ' + work['pin'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if pinchk.returncode == 0:
          hashlen=pinchk.stdout.decode().strip().split(' ')
          payload['pinned'] = hashlen[0] + '/' + work['pin']
          payload['length'] = hashlen[1]
        else:
          payload['error'] = pinchk.returncode
      else:
        payload['error'] = pincode

    if work['delete'] != '':
      #Delete/unpin any expired episodes
      logging.info('Unpinned old/expired hash (' + str(work['delete']) + ')')
      delete = subprocess.run(ipfspath + ' pin rm ' + work['delete'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      payload['deleted'] = work['delete']

    #Report Results
    logging.info('Reporting results...')
    #Get Usage/Available
    repostat = subprocess.run(ipfspath + ' repo stat -s|grep RepoSize', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if repostat.returncode == 0:
      repolen = repostat.stdout.decode().strip().split(':')
      used = int(repolen[1].strip())
    else:
      used = 0
    payload['used'] = used
    df = os.statvfs('/')
    payload['avail'] = df.f_bavail * df.f_frsize

    try:
      response = requests.post("https://IPFSPodcasting.net/Response", timeout=120, data=payload)
    except requests.RequestException as e:
      logging.info('Error sending response : ' + str(e))

  else:
    logging.info('No work.')

  #wait 10 minutes then start again
  logging.info('Sleeping 10 minutes...')
  time.sleep(600)


from flask_app import app, db, render_template, request, redirect, bcrypt, session, flash, url_for, EMAIL_REGEX, verify_logged_in, datetime, timedelta
from models import User, Movie, Post, Comment, favorites, post_likes, comment_likes, faved

@app.route('/')
def index():
    upms = db.session.query(User, Post, Movie).select_from(User).join(
        Post).join(Movie).where(Post.user_id == User.id and Post.movie_id == Movie.id).order_by(Post.created_at.desc()).all()
    # Set post timestamp
    for upm in upms:
        delta = (datetime.now() - upm[1].created_at)
        seconds = delta.seconds
        minutes = f"{seconds//60} min. ago"
        hours = f"{seconds//3600} hr. ago"
        days = f"{delta.days} d. ago"
        weeks = f"{delta.days//7} wk. ago"
        for unit in [weeks, days, hours, minutes]:
            if int(unit[:1]) > 0:
                upm[1].time_since = unit
                break
    return render_template('feed.html', upms=upms, truncate=True, title='Main Feed | ReDirector')

@app.route('/registration', methods=['POST', 'GET'])
def register():
    if request.method == 'POST':
        if not User.validate_user(request.form):
            return redirect('/registration')
        user = User(
            request.form['username'],
            bcrypt.generate_password_hash(request.form['password']),
            request.form['email'],
            request.form['first_name'],
            request.form['last_name']
        )
        db.session.add(user)
        db.session.commit()
        user = User.query.filter_by(
            email=request.form['email']).first()
        session['firt_name'] = user.first_name
        session['user_id'] = user.id
        session['username'] = user.username
        print(
            f"Logged in {session['first_name']} with ID {session['user_id']}")
        return redirect('/')
    else:
        return render_template('registration.html')

@app.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        # check if user used email to login
        if EMAIL_REGEX.match(request.form['username_email']):
            # check if email is in database
            user_in_db = User.query.filter_by(
                email=request.form['username_email']).first()
        else:
            # check if username is in database
            user_in_db = User.query.filter_by(
                username=request.form['username_email']).first()
        if not user_in_db:
            flash('Invalid Username/Email', 'username_email')
            return redirect('/login')
        # check password against stored hash
        if not bcrypt.check_password_hash(user_in_db.password, request.form['log_password']):
            flash('Invalid Password', 'log_password')
            return redirect('/login')
        # email and password are valid
        session['first_name'] = user_in_db.first_name
        session['user_id'] = user_in_db.id
        session['username'] = user_in_db.username
        print(
            f"Logged in {session['username']} ({session['first_name']})")
        return redirect(request.form['last_route'])
    else:
        return render_template('login.html', last_route=request.referrer)

@app.route('/logout')
def logout():
    print(f"Logged out {session['username']} ({session['first_name']})")
    session.pop('user_id', None)
    session.pop('first_name', None)
    session.pop('username', None)
    return redirect(request.referrer)


#!/usr/bin/env python3

import numpy as np
import operator

if __name__ == "__main__":
    log = None

    for l in open('3-input'):
        *l, = map(int, l.strip())
        if log is None:
            log = np.ndarray((1, len(l)), dtype=int)
            log[0] = l
        else:
            log = np.vstack([log, l])

    gamma = np.mean(log, axis=0) >= 0.5
    epsilon = np.mean(log, axis=0) <= 0.5

    part1 = int(''.join(np.char.mod('%d', gamma)), 2) * \
            int(''.join(np.char.mod('%d', epsilon)), 2)

    print('part1', part1)

    def filter(log: np.array, most: bool):
        c = 0
        while len(log) != 1 and c < len(log[0]):
            thres = np.mean(log[:, c], axis=0) >= 0.5
            if most:
                log = log[log[:, c] == thres]
            else:
                log = log[log[:, c] != thres]

            c += 1

        assert len(log) == 1

        return int(''.join(np.char.mod('%d', log[0])), 2)

    CO2 = filter(np.copy(log), True)
    O = filter(np.copy(log), False)

    part2 = O * CO2

    print('part2', part2)


"""Tutorial 8. Putting two plots on the screen

This tutorial sets up for showing how Chaco allows easily opening multiple
views into a single dataspace, which is demonstrated in later tutorials.
"""

from scipy import arange
from scipy.special import jn

from enable.api import ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import Item, View

from chaco.api import create_line_plot, HPlotContainer
from chaco.tools.api import PanTool

class PlotExample(HasTraits):
    container = Instance(HPlotContainer)

    traits_view = View(
        Item(
            "container",
            editor=ComponentEditor(),
            show_label=False,
            width=800,
            height=600,
        ),
        title="Chaco Tutorial",
    )

    def _container_default(self):
        x = arange(-5.0, 15.0, 20.0 / 100)

        y = jn(0, x)
        left_plot = create_line_plot(
            (x, y), bgcolor="white", add_grid=True, add_axis=True
        )
        left_plot.tools.append(PanTool(left_plot))
        self.left_plot = left_plot

        y = jn(1, x)
        right_plot = create_line_plot(
            (x, y), bgcolor="white", add_grid=True, add_axis=True
        )
        right_plot.tools.append(PanTool(right_plot))
        right_plot.y_axis.orientation = "right"
        self.right_plot = right_plot

        # Tone down the colors on the grids
        right_plot.hgrid.line_color = (0.3, 0.3, 0.3, 0.5)
        right_plot.vgrid.line_color = (0.3, 0.3, 0.3, 0.5)
        left_plot.hgrid.line_color = (0.3, 0.3, 0.3, 0.5)
        left_plot.vgrid.line_color = (0.3, 0.3, 0.3, 0.5)

        container = HPlotContainer(spacing=20, padding=50, bgcolor="lightgray")
        container.add(left_plot)
        container.add(right_plot)
        return container

demo = PlotExample()

if __name__ == "__main__":
    demo.configure_traits()


import datetime
import sqlite3
import os
import sys
from PyQt6.QtWidgets import *
from PyQt6.QtCore import Qt
from docxtpl import DocxTemplate

class mailbackGenWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Test Mailback Letter Generator")
        self.setFixedSize(722, 479)

        main_layout = QVBoxLayout()

        self.db = sqlite3.connect("test_mailback.db")
        self.cur = self.db.cursor()

        client_label = QLabel("Select Client: ")
        self.client_select = QComboBox()
        self.populateClientSelect()

        def setAndGet():
            self.getDefaultAddress()
            self.setDefaultAddress()

        self.client_select.currentIndexChanged.connect(setAndGet)

        reason_label = QLabel("Select All Reasons for Return ")
        self.reason_select = QFrame()
        self.reason_layout = QGridLayout()
        self.reasonCheckBoxList = []
        self.address1 = QLineEdit()
        self.address1.setFixedWidth(200)
        self.address2 = QLineEdit()
        self.address2.setFixedWidth(200)
        self.address3 = QLineEdit()
        self.address3.setFixedWidth(200)

        self.clear_address_button = QPushButton("Clear Address")
        self.clear_address_button.clicked.connect(self.clearAddress)
        self.default_address_button = QPushButton("Default")
        self.default_address_button.clicked.connect(self.setDefaultAddress)
        self.populateReasonLayout()
        self.reason_select.setLayout(self.reason_layout)

        self.reason_error = QLabel("Please select at least one reason.")
        self.reason_error.setStyleSheet("color: red")
        self.reason_error.hide()

        self.envelope_button = QPushButton("Generate Envelope")
        self.envelope_button.clicked.connect(self.printEnvelope)

        self.large_envelope_button = QPushButton("Large Envelope Sheet")
        self.large_envelope_button.clicked.connect(self.printLargeEnvelope)

        self.submit_button = QPushButton("Generate Letter")
        self.submit_button.clicked.connect(self.generateLetter)

        widgets = [client_label, self.client_select, reason_label, self.reason_select, self.reason_error,
                   self.submit_button, self.envelope_button, self.large_envelope_button]

        for w in widgets:
            main_layout.addWidget(w)
        widget = QWidget()
        widget.setLayout(main_layout)

        # Set the central widget of the Window. Widget will expand
        # to take up all the space in the window by default.
        self.setCentralWidget(widget)
        self.template = DocxTemplate("test_mailback_template.docx")

        self.envelope = DocxTemplate("mailout.docx")

        self.big_envelope = DocxTemplate("large envelope template.docx")

        self.current_date = datetime.date.today().strftime('%m/%d/%Y')

        self.currentClient = ""
        self.currentAddr1 = ""
        self.currentAddr2 = ""
        self.currentPhoneNumber = ""
        self.getDefaultAddress()
        self.setDefaultAddress()

    def populateClientSelect(self):
        tups = self.cur.execute("""SELECT query_name FROM client
                                ORDER BY query_name ASC;""")

        clients = [name for t in tups for name in t]
        self.client_select.addItems(clients)

    def getDefaultAddress(self):
        client_name = self.client_select.currentText()
        client_row = self.cur.execute("""SELECT full_name, address, phone_number
                                                    FROM client
                                                    WHERE query_name = ?""", (client_name,))
        self.currentClient, full_addr, self.currentPhoneNumber = [c for t in client_row for c in t]
        self.currentAddr1, self.currentAddr2 = full_addr.split('*')

    def setDefaultAddress(self):
        self.address1.setText(self.currentClient)
        self.address2.setText(self.currentAddr1)
        self.address3.setText(self.currentAddr2)

    def clearAddress(self):
        self.address1.clear()
        self.address2.clear()
        self.address3.clear()

    def populateReasonLayout(self):
        reasonTypes = self.cur.execute("""SELECT DISTINCT type FROM mailback_reason;""")
        reasonTypes = [t for rt in reasonTypes for t in rt]
        print(reasonTypes)

        column = 0
        row = 0
        for t in reasonTypes:
            if column == 2:
                column = 0
                row += 1

            frame = QFrame()
            layout = QVBoxLayout()
            layout.addWidget(QLabel(t + ':'))
            reasons = self.cur.execute("""SELECT reason FROM mailback_reason
                                            WHERE type = ?;""", (t,))
            reasons = [r for rt in reasons for r in rt]
            for r in reasons:
                box = QCheckBox(r)
                self.reasonCheckBoxList.append(box)
                layout.addWidget(box)
            frame.setLayout(layout)
            self.reason_layout.addWidget(frame, column, row, Qt.AlignmentFlag.AlignTop)

            column += 1

        if column == 2:
            column = 0
            row += 1

        frame = QFrame()
        layout = QGridLayout()
        layout.addWidget(QLabel('Name:'), 0, 0, Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(self.address1, 0, 1, Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(QLabel('Address:'), 1, 0, Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(self.address2, 1, 1, Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(QLabel('City/State/Zip:'), 2, 0, Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(self.address3, 2, 1, Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(self.clear_address_button, 3, 0, Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(self.default_address_button, 3, 1, Qt.AlignmentFlag.AlignLeft)
        frame.setLayout(layout)
        self.reason_layout.addWidget(frame, column, row, Qt.AlignmentFlag.AlignLeft)

    def generateLetter(self):
        #FOR SETTING FIXED WIDTH/HEIGHT
        #print(self.width())
        #print(self.height())

        # avoids Microsoft Word opening dialog box saying that letter.docx caused error
        if os.path.exists("letter.docx"):
            os.remove("letter.docx")

        reasons = []

        for box in self.reasonCheckBoxList:
            if box.isChecked():
                reasons.append(box.text())
                box.setChecked(False)

        reason = ""
        rlength = len(reasons)
        if rlength == 1:
            reason = reasons[0]
        elif rlength == 2:
            reason = reasons[0] + ' and ' + reasons[1]
        elif rlength > 2:
            for i in range(0, rlength):
                if i != rlength - 1:
                    reason += reasons[i] + ', '
                else:
                    reason += 'and ' + reasons[i]
        else:  # reasons is empty
            self.reason_error.show()
            return 1

        self.reason_error.hide()
        self.submit_button.setEnabled(False)

        fill_in = {"date": self.current_date,
                   "client": self.currentClient,
                   "reason": reason,
                   "address_1": self.currentAddr1,
                   "address_2": self.currentAddr2,
                   "phone_number": self.currentPhoneNumber
                   }

        self.template.render(fill_in)
        self.template.save('letter.docx')
        os.startfile("letter.docx", "print")

        self.submit_button.setEnabled(True)

    def printEnvelope(self):
        self.envelope_button.setEnabled(False)

        fill_in = {"client": self.address1.text(),
                   "addr_1": self.address2.text(),
                   "addr_2": self.address3.text()}

        self.envelope.render(fill_in)
        self.envelope.save('envelope.docx')
        os.startfile("envelope.docx", "print")

        self.envelope_button.setEnabled(True)

    def printLargeEnvelope(self):
        self.large_envelope_button.setEnabled(False)

        fill_in = {"client": self.address1.text(),
                   "addr_1": self.address2.text(),
                   "addr_2": self.address3.text()}

        self.big_envelope.render(fill_in)
        self.big_envelope.save('big_envelope.docx')
        os.startfile("big_envelope.docx", "print")

        self.large_envelope_button.setEnabled(True)

def main():
    app = QApplication(sys.argv)
    window = mailbackGenWindow()
    window.show()

    app.exec()

if __name__ == "__main__":
    main()


from waterworld.waterworld import env as custom_waterworld
from potential_field.potential_field_policy import PotentialFieldPolicy
from utils import get_frames

from pettingzoo.utils import average_total_reward

from multiprocessing import Pool, cpu_count

import tqdm
import numpy as np
from matplotlib import pyplot as plt
import json

n_coop_options = [1, 2]
n_sensor_options = [1, 2, 5, 20, 30]
angle_options = [("randomize_angle",False),("randomize_angle",True),
                ("spin_angle",0),("spin_angle",0.1),("spin_angle",0.5),("spin_angle",1)]
obs_weighting_options=[1, 0.5]
poison_weighting_options=[1, 0.5]
barrier_weighting_options=[1, 0.5]
food_weighting_options=[1, 0.5]

def test_policy(config, rounds=100):
    env = custom_waterworld(**config["env_config"])
    policy = PotentialFieldPolicy(**config["potential_field_config"]).get_movement_vector
    for i in tqdm.tqdm(range(rounds)):
        reward_sum, frame_list = get_frames(env, policy)
        config["rewards"].append(reward_sum)
    env.close()

    with open(f"potential_field/test_main/{config['config_index']}.json", "x") as f:
        json.dump(config, f, indent=4)

def get_configs():
    configs = []
    i=0
    for n_coop in n_coop_options:
        for n_sensor in n_sensor_options:
            for angle_config in angle_options:
                configs.append({"env_config":
                            {"n_coop": n_coop,"n_sensors": n_sensor,},
                            "potential_field_config":{
                                "n_sensors": n_sensor,
                                angle_config[0]: angle_config[1],
                            },
                                "rewards": [],
                                "config_index": i
                            })
                i += 1
        for obs_weight in obs_weighting_options:
            for poison_weight in poison_weighting_options:
                for barrier_weight in barrier_weighting_options:
                    for food_weight in food_weighting_options:
                        configs.append({"env_config":
                                    {"n_coop": n_coop,"n_sensors": 30,},
                                    "potential_field_config":{
                                        "n_sensors": 30,
                                        "obs_weight": obs_weight,
                                        "poison_weight": poison_weight,
                                        "barrier_weight": barrier_weight,
                                        "food_weight": food_weight
                                    },
                                        "rewards": [],
                                        "config_index": i
                                    })
                        i += 1
    return configs

def get_main_configs():
    configs = []
    i=0
    for n_coop in n_coop_options:
        for n_sensor in n_sensor_options:
            for angle_config in angle_options:
                configs.append({"env_config":
                            {"n_coop": n_coop,"n_sensors": n_sensor,},
                            "potential_field_config":{
                                "n_sensors": n_sensor,
                                angle_config[0]: angle_config[1],
                            },
                                "rewards": [],
                                "config_index": i
                            })
                i += 1

    return configs

def get_env_configs():
    configs = []
    i=0
    for n_coop in n_coop_options:
        for n_sensor in n_sensor_options:
                configs.append({"env_config":
                            {"n_coop": n_coop,"n_sensors": n_sensor,},
                                "rewards": [],
                                "config_index": i
                            })
                i += 1

    return configs

def test_random_env(config, rounds=100):
    env = custom_waterworld(**config["env_config"])
    action_space = env.action_space("pursuer_0")
    def policy(obs):
        return action_space.sample()

    for i in tqdm.tqdm(range(rounds)):
        reward_sum, frame_list = get_frames(env, policy)
        config["rewards"].append(reward_sum)
    env.close()

    with open(f"potential_field/test_random/{config['config_index']}.json", "x") as f:
        json.dump(config, f, indent=4)

if __name__ == "__main__":
    configs = get_env_configs()

    with Pool(processes=int(cpu_count() - 2)) as pool:
        for _ in tqdm.tqdm(pool.imap_unordered(test_random_env, configs), total=len(configs)):
            pass


from scipy.sparse import csr_matrix

from .text import WordPieceParser

from collections.abc import Mapping, Iterable

class RecordVectorMap(Mapping):

    def __init__(self, records, wp_model_path, vec_format='bag-of-words'):

        text_parser = WordPieceParser(wp_model_path)

        self.rec_seq_map, self.record_vecs = self.rec2vecs(records, text_parser, vec_format)

    def rec2vecs(self, records, text_parser, vec_format):

        rec_seq_map = {}
        cols, rows, data = [], [], []

        col_dim = 0 if vec_format=='sequence' else text_parser.vocab_size

        for rec_seq, (rec_id, rec_text) in enumerate(records):

            rec_seq_map[rec_id] = rec_seq

            parsed = text_parser.parse(rec_text, parse_format=vec_format)

            if vec_format=='sequence':

                if len(parsed)!=0:
                    rows.extend([rec_seq]*len(parsed))
                    cols.extend(list(range(len(parsed))))
                    data.extend(parsed)
                    if len(parsed)>col_dim:
                        col_dim = len(parsed)

            else:

                for wp_id, tf in parsed.items():
                    rows.append(rec_seq)
                    cols.append(wp_id)
                    data.append(tf)

        record_vecs = csr_matrix((data, (rows, cols)), shape=(len(records), col_dim))

        return rec_seq_map, record_vecs

    def __getitem__(self, key):
        if isinstance(key, str):
            return self.get_by_seqs(self.rec_seq_map[key])
        elif isinstance(key, Iterable):
            return self.get_by_seqs([self.rec_seq_map[a_key] for a_key in key])
        else:
            raise TypeError('Key must be string (key of record) or iterable (list of key of record).')

    def get_by_seqs(self, key):
        if isinstance(key, int):
            return self.record_vecs[key]
        elif isinstance(key, Iterable):
            return self.record_vecs[key]
        else:
            raise TypeError('Seqs must be int (seq of record) or iterable (list of seq of record).')

    def __iter__(self):
        return iter(self.record_vecs)

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


# #!/bin/python
# # -*- coding: utf8 -*-
# import sys
# import os
# import re

#请完成下面这个函数，实现题目要求的功能
#当然，你也可以不按照下面这个模板来作答，完全按照自己的想法来 ^-^
#******************************开始写代码******************************

def pathInZigZagTree(label):
    """
    Args:
        label: int

    Return:
        list[int]
    """
    level = 0
    count = 0
    while label > count:
        level += 1
        count += 2 ** (level - 1)
    res = []
    while level > 0:
        res.append(label)
        index = label2index(level, label)
        parent_index = (index + 1) // 2
        level -= 1
        label = index2label(level, parent_index)
    return res[::-1]

def label2index(level, label):
    """
    index 在该层的索引 从1开始

    Args:
        level: int
        label: int

    Return:
        int
    """
    if level % 2 == 0:
        return 2 ** level - label
    else:
        return label - 2 ** (level - 1) + 1

def index2label(level, index):
    """
    index 在该层的索引 从1开始

    Args:
        level: int
        index: int

    Return:
        int
    """
    if level % 2 == 0:
        return 2 ** level - index
    else:
        return 2 ** (level - 1) + index - 1

#******************************结束写代码******************************

if __name__ == "__main__":

    _label = int(input())
    # _label = 14

    res = pathInZigZagTree(_label)

    for res_cur in res:
        print(str(res_cur))


"""Adds column to use scicrunch alternative

Revision ID: b60363fe438f
Revises: 39fa67f45cc0
Create Date: 2020-12-15 18:26:25.552123+00:00

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "b60363fe438f"
down_revision = "39fa67f45cc0"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "group_classifiers",
        sa.Column("uses_scicrunch", sa.Boolean(), nullable=True, server_default="0"),
    )
    # ### end Alembic commands ###

    # Applies the default to all
    query = 'UPDATE "group_classifiers" SET uses_scicrunch=false;'
    op.execute(query)

    # makes non nullable
    # 'ALTER TABLE "group_classifiers" ALTER "uses_scicrunch" SET NOT NULL;'
    op.alter_column("group_classifiers", "uses_scicrunch", nullable=False)

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("group_classifiers", "uses_scicrunch")
    # ### end Alembic commands ###


import unittest
from unittest.mock import patch
import os
from typing import Optional
from dataclasses import dataclass
from io import StringIO

from ml_project.train_pipeline import run_train_pipeline
from ml_project.predict_pipeline import run_predict_pipeline
from sklearn.preprocessing import StandardScaler

from ml_project.entities import (
    TrainingPipelineParams,
    SplitParams,
    FeatureParams,
    TrainingParams
)

numerical_features = [
    "age",
    "sex",
    "cp",
    "trestbps",
    "chol",
    "fbs",
    "restecg",
    "thalach",
    "exang",
    "oldpeak",
    "slope",
    "ca",
    "thal",
]

@dataclass
class TestTrainingPipelineParams:
    input_data_path: str = "data/raw/heart_cleveland_upload.csv"
    output_model_path: str = "tests/tmp/test_model.pkl"
    metric_path: str = "tests/tmp/test_metrics.json"
    split_params: SplitParams = SplitParams(
        test_size=0.25,
        random_state=5
    )
    feature_params: FeatureParams = FeatureParams(
        numerical_features=numerical_features,
        target_col="condition"
    )
    train_params: TrainingParams = TrainingParams(
        model_type="RandomForestClassifier",
    )
    train_dataframe_path: Optional[str] = "data/raw/predict_dataset.csv"
    scaler: Optional[str] = None

@dataclass
class TestPredictPipelineParams:
    input_data_path: str = "data/raw/predict_dataset.csv"
    input_model_path: str = "models/model.pkl"
    output_data_path: str = "tests/tmp/test_model_predicts.csv"

class TestEnd2End(unittest.TestCase):
    test_train_piplein_params = TestTrainingPipelineParams()
    test_test_piplein_params = TestPredictPipelineParams()

    @unittest.mock.patch("ml_project.train_pipeline.logger")
    def test_train_end2end(self, mock_log):
        with patch("sys.stdout", new=StringIO()):
            path_to_model, metrics = run_train_pipeline(self.test_train_piplein_params)

            self.assertTrue(os.path.exists(path_to_model))
            self.assertTrue(metrics["0"]["f1-score"] > 0.6)
            self.assertTrue(metrics["1"]["f1-score"] > 0.6)

    @unittest.mock.patch("ml_project.train_pipeline.logger")
    def test_predict_end2end(self, mock_log):
        with patch("sys.stdout", new=StringIO()):
            run_predict_pipeline(self.test_test_piplein_params)

            self.assertTrue(os.path.exists(self.test_test_piplein_params.output_data_path))


import json
import os
import docx

with open(f'disciplinas.json') as f:
    data = json.load(f)

# print(df.columns.values)
for index, discpln in data.items():

    print(f'{discpln["sigla"]} - {discpln["nome"]}')

    doc = docx.Document()
    doc.add_heading(f'{discpln["sigla"]} - {discpln["nome"]}')
    doc.add_heading(f'{discpln["nome_en"]}', level=3)
    doc.add_paragraph()

    # Dados gerais
    p = doc.add_paragraph(style = 'List Bullet')
    p.add_run(f'Créditos-aula: {discpln["CA"]}\n')
    p.add_run(f'Créditos-trabalho: {discpln["CT"]}\n')
    p.add_run(f'Carga horária: {discpln["CH"]}\n')
    p.add_run(f'Ativação: {discpln["ativacao"]}\n')
    p.add_run(f'Departamento: {discpln["departamento"]}\n')

    # Cursos e semestres ideais
    cs = f'Curso (semestre ideal):'
    for curso, semestre in discpln["semestre"].items():
        cs += f' {curso} ({semestre}),'
    p.add_run(cs[:-1])

    # Objetivos
    doc.add_heading(f'Objetivos', level=2)
    doc.add_paragraph(f'{discpln["objetivos"]}')
    if discpln["abstract"]:
        p = doc.add_paragraph()
        p.add_run(f'{discpln["objectives"]}').italic = True

    # Docentes
    doc.add_heading(f'Docente(s) Responsável(eis) ', level=2)
    profs = discpln["docentes"]
    nprofs = discpln["ndoc"]
    if nprofs:
        p = doc.add_paragraph(style='List Bullet')
        for i in range(nprofs-1):
            p.add_run(f'{profs[i]}\n')
        p.add_run(f'{profs[-1]}')

    # programa resumido
    doc.add_heading(f'Programa resumido', level=2)
    doc.add_paragraph(f'{discpln["resumo"]}')
    if discpln["abstract"]:
        p = doc.add_paragraph()
        p.add_run(f'{discpln["abstract"]}').italic = True

    # programa
    doc.add_heading(f'Programa', level=2)
    doc.add_paragraph(f'{discpln["programa"]}')
    if discpln["program"]:
        p = doc.add_paragraph()
        p.add_run(f'{discpln["program"]}').italic = True

    # avaliação
    doc.add_heading('Avaliação', level=2)
    p = doc.add_paragraph( style='List Bullet')
    p.add_run('Método: ').bold = True
    p.add_run(f'{discpln["metodo"]}\n')
    p.add_run('Critério: ').bold = True
    p.add_run(f'{discpln["criterio"]}\n')
    p.add_run('Norma de recuperação: ').bold = True
    p.add_run(f'{discpln["exame"]}')

    # bibliografia
    doc.add_heading('Bibliografia', level=2)
    doc.add_paragraph(f'{discpln["bibliografia"]}')

    # Requisitos
    nr = discpln['requisitos']
    if nr:
        doc.add_heading('Requisitos', level=2)
        p = doc.add_paragraph(style='List Bullet')
        for k, req in nr.items():
            p.add_run(f"{req['sigla']} - {req['nome']} ({req['tipo']})\n")

    # salvando
    try:
        os.mkdir(f'../assets/disciplinas/')
    except  FileExistsError:
        pass
    docname = f'../assets/disciplinas/{discpln["sigla"]}.docx'
    doc.save(docname)

    # exportando pdf
    os.system(f'abiword --to=pdf {docname}')

    # break


import time
from functools import wraps

from MatrixDecomposition import MatrixDecomposition
from MatrixGeneration import MatrixGeneration

def fn_timer(function):
    @wraps(function)
    def function_timer(*args, **kwargs):
        t0 = time.time()
        result = function(*args, **kwargs)
        t1 = time.time()
        print("Total time running '%s': %s seconds" % (function.__name__, str(t1 - t0)))
        return result

    return function_timer

class Analyzer:

    @staticmethod
    def analyze_tridiagonal():
        Analyzer.analyze_gauss(10, MatrixGeneration.tridiagonal)
        Analyzer.analyze_seidel(10, MatrixGeneration.tridiagonal)
        Analyzer.analyze_gauss(50, MatrixGeneration.tridiagonal)
        Analyzer.analyze_seidel(50, MatrixGeneration.tridiagonal)
        Analyzer.analyze_gauss(100, MatrixGeneration.tridiagonal)
        Analyzer.analyze_seidel(100, MatrixGeneration.tridiagonal)

    @staticmethod
    def analyze_hilbert():
        Analyzer.analyze_gauss(10, MatrixGeneration.hilbert)
        Analyzer.analyze_seidel(10, MatrixGeneration.hilbert)
        Analyzer.analyze_gauss(50, MatrixGeneration.hilbert)
        Analyzer.analyze_seidel(50, MatrixGeneration.hilbert)
        Analyzer.analyze_gauss(100, MatrixGeneration.hilbert)
        Analyzer.analyze_seidel(100, MatrixGeneration.hilbert)

    @staticmethod
    @fn_timer
    def analyze_gauss(n, method):
        print(f'\'{method.__name__}\' {n}')
        matrix = method(n)
        right = MatrixGeneration.right(matrix)
        matrix_decomposition = MatrixDecomposition(matrix)
        gauss = matrix_decomposition.solve_by_gauss(right)

    @staticmethod
    @fn_timer
    def analyze_seidel(n, method):
        print(f'\'{method.__name__}\' {n}')
        matrix = method(n)
        right = MatrixGeneration.right(matrix)
        matrix_decomposition = MatrixDecomposition(matrix)
        seidel = matrix_decomposition.solve_by_seidel(right, 1e-3)


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

# In[ ]:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
import random

def get_sore_and_Price(store_id,internet_id):
    driver = webdriver.Chrome('C:/Users/cunzh/Desktop/chromedriver.exe') ## you should change the path before you run
    start_page = driver.get("https://www.homedepot.com/l/")
    driver.find_element_by_id("storeSearchBox").send_keys(store_id)
    driver.find_element_by_class_name("sfSearchbox__button").click()
    time.sleep(random.randint(3,5))
    Message=''
    try:
        store = driver.find_element_by_class_name('sfstores')
        store_name = store.find_element_by_class_name('sfstorename').text
    #print(store.get_attribute("outerHTML"))
    except:
        price="NA"
        Message="store cannot be found"
    else:

        a = store.find_element_by_class_name('sfstorelinks').find_element_by_tag_name('a')
        time.sleep(random.randint(3,5))   #time.sleep are pretening human behavior; human spend different time on different web. So this website will not recognize this as a bot.
        a.click()                         #Randint gives us random integer 3 to 5
        time.sleep(random.randint(3,5))

        driver.find_element_by_id("headerSearch").send_keys(internet_id)
        time.sleep(random.randint(3,5))
        driver.find_element_by_id("headerSearchButton").click()
        time.sleep(random.randint(3,5))
        try:
            content = driver.find_element_by_class_name("price-detailed__wrapper")
            # print(content.get_attribute('innerHTML'))
            spans = content.find_elements_by_tag_name('span')
            if len(spans) != 3:
                price='NA'
                Message='price cannot be found'
            else:
                a = spans[1]
                b = spans[2]
                price = a.text + '.' + b.text

        except:
            price='NA'
            Message='price cannot be found'

    return store_id,price,Message

# In[ ]:

# We can test the code by using follwing example:
store_list = ['954', '907', '6917']
test_list = ['302895490', '302895488', '100561401', '206809290']
list1=[]
for store in store_list:
        for item in test_list:
            list1.append(get_sore_and_Price(store,item))

# In[ ]:

list1


# Builds some spectra and self-energies using the aux.Aux functionality

import numpy as np
from auxgf import mol, hf, aux, agf2, grids
from auxgf.util import Timer

timer = Timer()

# Build the Molecule object:
m = mol.Molecule(atoms='H 0 0 0; Li 0 0 1.64', basis='cc-pvdz')

# Build the RHF object:
rhf = hf.RHF(m)
rhf.run()

# Build the grid:
refq = grids.ReFqGrid(2**8, minpt=-5, maxpt=5, eta=0.1)

# Build the Hartree-Fock Green's function:
g_hf = aux.Aux(np.zeros(0), np.zeros((rhf.nao, 0)), chempot=rhf.chempot)

# Build the MP2 self-energy:
s_mp2 = aux.build_rmp2_iter(g_hf, rhf.fock_mo, rhf.eri_mo)

# Build the second-iteration Green's function, which corresponds to the QP spectrum at MP2 level or G^(2):
e, c = s_mp2.eig(rhf.fock_mo)
g_2 = g_hf.new(e, c[:rhf.nao])  # inherits g_hf.chempot

# Run an RAGF2 calcuation and get the converged Green's function and self-energy (we also use the RAGF2 density):
gf2 = agf2.RAGF2(rhf, nmom=(2,3), verbose=False)
gf2.run()
s_gf2 = gf2.se
e, c = s_gf2.eig(rhf.get_fock(gf2.rdm1, basis='mo'))
g_gf2 = s_gf2.new(e, c[:rhf.nao])

# For each Green's function, get the spectrum (Aux.as_spectrum only represents the function on a grid, we must
# also provide ordering='retarded' and then refactor):
def aux_to_spectrum(g):
    a = g.as_spectrum(refq, ordering='retarded')
    a = a.imag / np.pi
    a = a.trace(axis1=1, axis2=2)
    return a

a_hf = aux_to_spectrum(g_hf)
a_2 = aux_to_spectrum(g_2)
a_gf2 = aux_to_spectrum(g_gf2)

# Compare the spectra quantitatively:
print('| A(hf) - A(2) |   = %.12f' % (np.linalg.norm(a_hf - a_2) / refq.npts))
print('| A(gf2) - A(2) |  = %.12f' % (np.linalg.norm(a_gf2 - a_2) / refq.npts))
print('| A(gf2) - A(hf) | = %.12f' % (np.linalg.norm(a_gf2 - a_hf) / refq.npts))

print('time elapsed: %d min %.4f s' % (timer.total() // 60, timer.total() % 60))


import numpy as np
import matplotlib
from matplotlib.colors import ListedColormap

SLACred = '#8C1515'
SLACgrey = '#53565A'
SLACblue = '#007C92'
SLACteal = '#279989'
SLACgreen = '#8BC751'
SLACyellow = '#FEDD5C'
SLACorange = '#E04F39'
SLACpurple = '#53284F'
SLAClavender = '#765E99'
SLACbrown = '#5F574F'

SLACcolors = [SLACred,
              SLACblue,
              SLACteal,
              SLACgreen,
              SLACyellow,
              SLACgrey,
              SLACorange,
              SLACpurple,
              SLAClavender,
              SLACbrown,
]

# SLACsage = [199./256, 209./256, 197./256]
white = [256./256, 256./256, 256./256]
SLACpaloverde = [39./256, 153./256, 137./256]

matplotlib.cm.register_cmap('SLACverde',
                            ListedColormap(np.array([np.interp(np.linspace(0, 1, 256),
                                                               [0, 1],
                                                               [whiteV, pvV])
                                                     for whiteV, pvV in zip(white, SLACpaloverde)]).T,
                                           name = 'SLACverde'))

LaTeXflavor = {"numu": r'$\nu_\mu$',
               "numubar": r'$\bar{\nu}_\mu$',
               "nue": r'$\nu_e$',
               "nuebar": r'$\bar{\nu}_e$',
               "nutau": r'$\nu_\tau$',
               "nutaubar": r'$\bar{\nu}_\tau$'}

matplotlib.rc('axes', **{"prop_cycle": matplotlib.cycler(color = SLACcolors)})
matplotlib.rc('image', **{"cmap": 'SLACverde'})
matplotlib.rc('font', **{"family": 'sans-serif',
                         "sans-serif": 'Arial',
                         "size": 16,
                         "weight": 'bold'})
matplotlib.rc('text', **{"usetex": True})