import sys
from datetime import datetime
import json
import utilities
import numpy as np

#---------------------------------
#python3 main.py  ./crimes-new-york-city/NYPD_Complaint_Data_Historic.csv

#Uncomment this if you want to output a json file dump of dateString => noOfCrimes for that date

# pathToNYPDCrimeDataset = sys.argv[1]
# f = open(pathToNYPDCrimeDataset, "r")
# firstLine = True
# headerLine = None
# currentDate = None
# dateToNoOfCrimesDict = {} #maps dateString => noOfCrimes
# for line in f:
#    if firstLine == True:
#       headerLine = line
#       firstLine = False
#    else:
#       tokens = line.split(",")
#       dateString = tokens[1].strip()
#       if "2015" not in dateString and "2014" not in dateString:
#          continue
#       try:
#          currentLineDate = datetime.strptime(dateString, "%m/%d/%Y").date()
#          utilities.addCountInDictionaryForKey(dateToNoOfCrimesDict, dateString)
#       except:
#          continue

# with open("./dateToNoOfCrimesDict.json", "w") as dateToNoOfCrimesDictFileHandler:
#    json.dump(dateToNoOfCrimesDict, dateToNoOfCrimesDictFileHandler)
# f.close()
#---------------------------------

#1048576 lines
# with open("./dateToNoOfCrimesDict.json") as dateToNoOfCrimesDictFileHandler:
#    dateToNoOfCrimesDict = json.load(dateToNoOfCrimesDictFileHandler)
#    summation = 0
#    for key, value in dateToNoOfCrimesDict.items():
#       print(key + " " + str(value))
#       summation += value
#    print(summation)

#---------------------------------
#/Users/joewijoyo/anaconda3/bin/python3.7 main.py ./historical-hourly-weather-data/temperature.csv ./historical-hourly-weather-data/wind_speed.csv ./historical-hourly-weather-data/humidity.csv ./historical-hourly-weather-data/weather_description.csv historical-hourly-weather-data/pressure.csv historical-hourly-weather-data/wind_direction.csv

#Uncomment this to get dateToWeatherDataDict outputted to the current directory as a json dump file

# WEATHER_DATASET_DATE_COLUMN_INDEX = 0
# WEATHER_DATASET_NEW_YORK_COLUMN_INDEX = 28

# def getWeatherDataFromWeatherCSVFile(pathToWeatherDataset, dateToWeatherDataDict, keyString):
#    firstLine = True
#    f = open(pathToWeatherDataset, "r")
#    for line in f:
#       if firstLine == True:
#          headerLine = line
#          firstLine = False
#          #print(headerLine.split(",")[WEATHER_DATASET_NEW_YORK_COLUMN_INDEX])
#       else:
#          tokens = line.split(",")
#          dateString = tokens[WEATHER_DATASET_DATE_COLUMN_INDEX].split(" ")[0]
#          if "2015" not in dateString and "2014" not in dateString:
#             continue
#          try:
#             currentLineDate = datetime.strptime(dateString, "%Y-%m-%d").date()
#             currentLineDate = datetime.strftime(currentLineDate, "%m/%d/%Y")
#             if currentLineDate not in dateToWeatherDataDict:
#                dateToWeatherDataDict[currentLineDate] = {
#                   "humidityValuesList" : [],
#                   "pressureValuesList" : [],
#                   "temperatureValuesList" : [],
#                   "weatherDescriptionValuesList" : [],
#                   "windDirectionValuesList" : [],
#                   "windSpeedValuesList" : []
#                }
#             appendValue = None
#             try:
#                appendValue = float(tokens[WEATHER_DATASET_NEW_YORK_COLUMN_INDEX])
#             except:
#                appendValue = tokens[WEATHER_DATASET_NEW_YORK_COLUMN_INDEX].strip()
#             if appendValue != "":
#                dateToWeatherDataDict[currentLineDate][keyString].append(appendValue)

#          except:
#             continue

#    f.close()

# dateToWeatherDataDict = {}
# """
#    dateString => {
#       humidity:
#       pressure:
#       temperature:
#       weatherDescription:
#       windDirection:
#       windSpeed:
#    }
# """
# for pathToWeatherDataset in sys.argv[1:]:
#    keyString = None
#    if "temperature" in pathToWeatherDataset:
#       keyString = "temperatureValuesList"
#    elif "wind_speed" in pathToWeatherDataset:
#       keyString = "windSpeedValuesList"
#    elif "humidity" in pathToWeatherDataset:
#       keyString = "humidityValuesList"
#    elif "weather_description" in pathToWeatherDataset:
#       keyString = "weatherDescriptionValuesList"
#    elif "pressure" in pathToWeatherDataset:
#       keyString = "pressureValuesList"
#    else:
#       keyString = "windDirectionValuesList"
#    getWeatherDataFromWeatherCSVFile(pathToWeatherDataset, dateToWeatherDataDict, keyString)

# for dateString, weatherDataDict in dateToWeatherDataDict.items():
#    dateToWeatherDataDict[dateString]["humidity"] = np.mean(weatherDataDict["humidityValuesList"])
#    dateToWeatherDataDict[dateString]["temperature"] = np.mean(weatherDataDict["temperatureValuesList"])
#    dateToWeatherDataDict[dateString]["windSpeed"] = np.mean(weatherDataDict["windSpeedValuesList"])
#    dateToWeatherDataDict[dateString]["weatherDescription"] = utilities.getMostAppearingValueInList(weatherDataDict["weatherDescriptionValuesList"])
#    dateToWeatherDataDict[dateString]["pressure"] = np.mean(weatherDataDict["pressureValuesList"])
#    dateToWeatherDataDict[dateString]["windDirection"] = np.mean(weatherDataDict["windDirectionValuesList"])
#    del(weatherDataDict["humidityValuesList"])
#    del(weatherDataDict["temperatureValuesList"])
#    del(weatherDataDict["windSpeedValuesList"])
#    del(weatherDataDict["weatherDescriptionValuesList"])
#    del(weatherDataDict["pressureValuesList"])
#    del(weatherDataDict["windDirectionValuesList"])
#    print(weatherDataDict)

# with open("./dateToWeatherDataDict.json", "w") as dateToWeatherDataDictFileHandler:
#     json.dump(dateToWeatherDataDict, dateToWeatherDataDictFileHandler)

#need to make a csv file where weather attributes are on the line and
#---------------------------------

import xml.etree.ElementTree
import xml.etree.cElementTree as ET

dateToWeatherDataDict = {}
with open("./dateToWeatherDataDict.json") as dateToWeatherDataDictFileHandler:
   dateToWeatherDataDict = json.load(dateToWeatherDataDictFileHandler)

dateToNoOfCrimesDict = {}
with open("./dateToNoOfCrimesDict.json") as dateToNoOfCrimesDictFileHandler:
   dateToNoOfCrimesDict = json.load(dateToNoOfCrimesDictFileHandler)

allUniqueValuesDict = {
   "humidity" : [],
   "temperature" : [],
   "windSpeed" : [],
   "weatherDescription" : [],
   "pressure" : [],
   "windDirection" : [],
   "crimeRate" : [],
} #maps column => [unique values of column]
count = 0
noOfLessThanOrEqualTo800 = 0
noOf801To1000 = 0
noOf1001To1200 = 0
noOf1201To1400 = 0
noOfGreaterThan1400 = 0
for dateString, noOfCrimes in dateToNoOfCrimesDict.items():
   if dateString in dateToWeatherDataDict:
      weatherDataDict = dateToWeatherDataDict[dateString]
      crimeRateString = None
      if noOfCrimes <= 800:
         crimeRateString = "<=800"
         noOfLessThanOrEqualTo800 += 1
      elif noOfCrimes <= 1000:
         crimeRateString = "801-1000"
         noOf801To1000 += 1
      elif noOfCrimes <= 1200:
         crimeRateString = "1001-1200"
         noOf1001To1200 += 1
      elif noOfCrimes <= 1400:
         crimeRateString = "1201-1400"
         noOf1201To1400 += 1
      else:
         crimeRateString = ">1400"
         noOfGreaterThan1400 += 1
      if str(weatherDataDict["weatherDescription"]) not in allUniqueValuesDict["weatherDescription"]:
         allUniqueValuesDict["weatherDescription"].append(str(weatherDataDict["weatherDescription"]))
      if crimeRateString not in allUniqueValuesDict["crimeRate"]:
         allUniqueValuesDict["crimeRate"].append(crimeRateString)
      count += 1
print(count)
print("No of <=800: " + str(noOfLessThanOrEqualTo800))
print("No of 801-1000: " + str(noOf801To1000))
print("No of 1001-1200: " + str(noOf1001To1200))
print("No of 1201-1400: " + str(noOf1201To1400))
print("No of >1400: " + str(noOfGreaterThan1400))

# with open("./weatherDataAndNoOfCrimesData.csv", "w") as outputCSVFileHandler:
#    outputCSVFileHandler.write("humidity,temperature,windSpeed,weatherDescription,pressure,windDirection,crimeRate\n")
#    outputCSVFileHandler.write("1,1,1,1,1,1,1\n")
#    outputCSVFileHandler.write("crimeRate\n")
#    for dateString, noOfCrimes in dateToNoOfCrimesDict.items():
#       if dateString in dateToWeatherDataDict:
#          crimeRateString = None
#          if noOfCrimes <= 800:
#             crimeRateString = "<=800"
#          elif noOfCrimes <= 1000:
#             crimeRateString = "801-1000"
#          elif noOfCrimes <= 1200:
#             crimeRateString = "1001-1200"
#          elif noOfCrimes <= 1400:
#             crimeRateString = "1201-1400"
#          else:
#             crimeRateString = ">1400"
#          weatherDataDict = dateToWeatherDataDict[dateString]
#          writeString = str(weatherDataDict["humidity"]) + "," + str(weatherDataDict["temperature"]) + "," \
#                           + str(weatherDataDict["windSpeed"]) + "," + str(allUniqueValuesDict["weatherDescription"].index(weatherDataDict["weatherDescription"]) + 1) + "," \
#                           + str(weatherDataDict["pressure"]) + "," + str(weatherDataDict["windDirection"]) + "," \
#                           + str(allUniqueValuesDict["crimeRate"].index(crimeRateString) + 1) + "\n"
#          outputCSVFileHandler.write(writeString)
#          print(noOfCrimes)

#          if str(weatherDataDict["humidity"]) not in allUniqueValuesDict["humidity"]:
#             allUniqueValuesDict["humidity"].append(str(weatherDataDict["humidity"]))
#          if str(weatherDataDict["temperature"]) not in allUniqueValuesDict["temperature"]:
#             allUniqueValuesDict["temperature"].append(str(weatherDataDict["temperature"]))
#          if str(weatherDataDict["windSpeed"]) not in allUniqueValuesDict["windSpeed"]:
#             allUniqueValuesDict["windSpeed"].append(str(weatherDataDict["windSpeed"]))
#          # if str(weatherDataDict["weatherDescription"]) not in allUniqueValuesDict["weatherDescription"]:
#          #    allUniqueValuesDict["weatherDescription"].append(str(weatherDataDict["weatherDescription"]))
#          if str(weatherDataDict["pressure"]) not in allUniqueValuesDict["pressure"]:
#             allUniqueValuesDict["pressure"].append(str(weatherDataDict["pressure"]))
#          if str(weatherDataDict["windDirection"]) not in allUniqueValuesDict["windDirection"]:
#             allUniqueValuesDict["windDirection"].append(str(weatherDataDict["windDirection"]))
#          # if crimeRateString not in allUniqueValuesDict["crimeRate"]:
#          #    allUniqueValuesDict["crimeRate"].append(crimeRateString)

#          #outputCSVFileHandler.write("")

# #CODE BELOW TO CREATE XML DOMAIN FILE
# domainRootElement = ET.Element("domain")

# humidityElement = ET.SubElement(domainRootElement, "variable", name="humidity", continuous="true")
# for uniqueElement in allUniqueValuesDict["humidity"]:
#    ET.SubElement(humidityElement, "group", name=uniqueElement, p=".00")

# temperatureElement = ET.SubElement(domainRootElement, "variable", name="temperature", continuous="true")
# for uniqueElement in allUniqueValuesDict["temperature"]:
#    ET.SubElement(temperatureElement, "group", name=uniqueElement, p=".00")

# windSpeedElement = ET.SubElement(domainRootElement, "variable", name="windSpeed", continuous="true")
# for uniqueElement in allUniqueValuesDict["windSpeed"]:
#    ET.SubElement(windSpeedElement, "group", name=uniqueElement, p=".00")

# weatherDescriptionElement = ET.SubElement(domainRootElement, "variable", name="weatherDescription")
# for uniqueElement in allUniqueValuesDict["weatherDescription"]:
#    ET.SubElement(weatherDescriptionElement, "group", name=uniqueElement, p=".00")

# pressureElement = ET.SubElement(domainRootElement, "variable", name="pressure", continuous="true")
# for uniqueElement in allUniqueValuesDict["pressure"]:
#    ET.SubElement(pressureElement, "group", name=uniqueElement, p=".00")

# windDirectionElement = ET.SubElement(domainRootElement, "variable", name="windDirection", continuous="true")
# for uniqueElement in allUniqueValuesDict["windDirection"]:
#    ET.SubElement(windDirectionElement, "group", name=uniqueElement, p=".00")

# crimeRateElement = ET.SubElement(domainRootElement, "Category", name="crimeRate")
# for index, uniqueElement in enumerate(allUniqueValuesDict["crimeRate"]):
#    ET.SubElement(crimeRateElement, "choice", name=uniqueElement, type=str(index))

# ET.ElementTree(domainRootElement).write("weatherDataAndNoOfCrimesDataDomain.xml")


# 징검다리 (11561번)
##############################################################################
    # 문제: https://www.acmicpc.net/problem/11561
    # 이분탐색
    # 규칙을 지키면서 밟을 수 있는 징검다리 수의 최대를 구하는 문제
    # 1번 규칙에 의해 첫번째 점프는 원하는 만큼 건너 뛰고, 그 다음부터는 이전 점프에서 + 1씩 증가시킨 점프를 뛰어야한다.
    # mid 값을 마지막으로 점프한 칸 수라고 할 때, 최대한 밟을 수 있는 징검다리 수는 1칸씩 증가하는 등차 수열의 합으로
    # 1칸 + 2칸 + 3칸 + ... + mid칸이다. 즉, 이는 mid(mid + 1) / 2 로, 해당 값이 N보다 작거나 같은 mid를 구하면 된다.
##############################################################################
import sys
input = sys.stdin.readline

def binary_search(N):
    opt = 0
    s, e = 1, N
    while s <= e:
        mid = (s + e) // 2
        ret = (mid * (mid + 1)) / 2
        if ret <= N:
            s = mid + 1
            opt = mid
        else:
            e = mid - 1
    return opt

T = int(input().rstrip())
for tc in range(T):
    N = int(input().rstrip())
    print(binary_search(N))


# coding:utf-8
import pytest
import time
from Cloud_PyTest.pytest_rusi.page.login_page import _login,_login_user_result_error,_login_pwd_result_error,_login_submit_result_error

class TestLogin():

    @pytest.fixture(scope="function",  autouse=True)
    def startPage(self, driver, host):
        print("---让每个用例都从登录首页开始:---start!---")
        driver.get(host+"/user/login")
        driver.delete_all_cookies()
        driver.refresh()

    def test_login_1_fail(self, driver, host):
        """测试用例1：用户名空，密码正确"""
        _login(driver, host, "", "111111")
        # time.sleep(2)
        result1 = _login_user_result_error(driver)
        if  result1 == "请输入用户名":
            print("断言失败")
        else:
            print("断言成功")

    def test_login_2_fail(self, driver, host):
        '''测试用例2：用户名正确，密码为空'''
        _login(driver, host, "test0012", "")
        # time.sleep(2)
        result2 = _login_pwd_result_error(driver)
        if  result2 == "请输入密码":
            print("断言失败")
        else:
            print("断言成功")

    def test_login_3_fail(self, driver, host):
        '''测试用例3：用户名 密码都为空'''
        _login(driver, host, "", "")
        # time.sleep(2)
        result3 = _login_user_result_error(driver)
        result3_1 = _login_pwd_result_error(driver)

        if  result3 == "请输入用户名":
            print("断言失败")
        else:
            print("断言成功")
        if  result3_1 == "请输入密码":
            print("断言失败")
        else:
            print("断言成功")

    def test_login_4_fail(self, driver, host):
        '''测试用例4：用户名为特殊字符(*- $%^&./';l)+数字 密码正确'''
        _login(driver, host, "*- $%^&./';l12", "111111")
        # time.sleep(1)
        result4 = _login_submit_result_error(driver)
        if  result4 == "用户名不存在":
            print("断言失败")
        else:
            print("断言成功")

    def test_login_5_fail(self, driver, host):
        '''测试用例5：用户名正确 密码为特殊字符(*- $%^&./';l)+数字'''
        _login(driver, host, "test0012", "*- $%^&./';l12")
        # time.sleep(1)
        result5 = _login_submit_result_error(driver)
        if  result5 == "用户密码错误，请重试！":
            print("断言失败")
        else:
            print("断言成功")

    def test_login_6_fail(self, driver, host):
        '''测试用例6：用户名输入超长(数字+字母) 密码为特殊字符(*- $%^&./';l)+数字'''
        _login(driver, host, "test0012", "*- $%^&./';l12")
        # time.sleep(1)
        result6 = _login_submit_result_error(driver)
        if  result6 == "用户密码错误，请重试！":
            print("断言失败")
        else:
            print("断言成功")

    def test_login_7_fail(self, driver, host):
        '''测试用例7：用户名输入特殊字符(*- $%^&./';l)+数字 密码为超长(数字+字母)'''
        _login(driver, host, "*- $%^&./';l456", "123456zxcasdwrew")
        # time.sleep(1)
        result7 = _login_submit_result_error(driver)
        if  result7 == "用户名不存在":
            print("断言失败")
        else:
            print("断言成功")

    def test_login_8_fail(self, driver, host):
        '''测试用例8：用户名输入1位数字 密码输入1位数字'''
        _login(driver, host, "1", "1")
        # time.sleep(1)
        result8 = _login_submit_result_error(driver)
        if  result8 == "用户密码错误，请重试！":
            print("断言失败")
        else:
            print("断言成功")

    def test_login_9_fail(self, driver, host):
        '''测试用例9：用户名输入1位字母(不区分大小写) 密码输入1位字母(不区分大小写)'''
        _login(driver, host, "a", "D")
        # time.sleep(1)
        result9 = _login_submit_result_error(driver)
        if  result9 == "用户密码错误，请重试！":
            print("断言失败")
        else:
            print("断言成功")

    def test_login_10_fail(self, driver, host):
        '''测试用例9：用户名输入1位特殊字符 密码输入1位特殊字符'''
        _login(driver, host, "#", "%")
        # time.sleep(1)
        result10 = _login_submit_result_error(driver)
        if  result10 == "用户密码错误，请重试！":
            print("断言失败")
        else:
            print("断言成功")

    def test_login_11_pass(self, driver, host):
        '''测试用例9：用户名正确 密码正确'''
        _login(driver, host, "test0012", "111111")

if __name__ == "__main__":
    pytest.main(["-s","-v", "test_login.py","--html=report.html"])


lines = []
acc = 0
executed = set()

def isInfinite(lines):
    global acc
    acc = 0
    executed = set()
    currentLine = 0
    while currentLine < len(lines):
        executed.add(currentLine)
        nextLine = currentLine + 1
        line = lines[currentLine]
        pieces = line.split(' ')
        op = pieces[0]
        val = pieces[1]
        if val[1] == '+':
            val = val[1:]
        val = int(val)

        if op == "jmp":
            nextLine = currentLine + val
        elif op == "acc":
            acc += val

        if nextLine in executed:
            return True

        currentLine = nextLine
    return False

with open('input.txt') as input:
    for line in input:
        lines.append(line)
    currentLine = 0
    originalLine = ""
    while True:
        line = lines[currentLine]
        originalLine = line
        pieces = line.split(' ')
        op = pieces[0]

        if op == "jmp":
            lines[currentLine] = lines[currentLine].replace("jmp", "nop")
        elif op == "nop":
            lines[currentLine] = lines[currentLine].replace("nop", "jmp")

        if not isInfinite(lines):
            break
        else:
            lines[currentLine] = originalLine

        currentLine += 1

print(acc)


import typing
import uuid
from dataclasses import dataclass, field
from enum import IntEnum, auto

from .message import Message, Trace

@dataclass(frozen=True, kw_only=True)
class CommandReply(Message):
    """
    A command reply message.

    Every command needs to receive a reply in the form of a ``CommandReply`` message. The reply contains
    information about its ``success``, as well as a text message which is usually used to describe reasons for
    failures.

    Attributes:
        success: Whether the command succeeded.
        message: Arbitrary text, usually used to describe reasons for failures.
        unique: The unique identifier of its corresponding command.
    """
    class FailType(IntEnum):
        """
        Used when a command failed.
        """
        NONE = 0
        TIMEOUT = auto()
        EXCEPTION = auto()
        UNKNOWN = auto()

    success: bool = True
    message: str = ""

    unique: Trace = field(default_factory=uuid.uuid4)

CommandReplyType = typing.TypeVar("CommandReplyType", bound=CommandReply)  # pylint: disable=invalid-name

CommandDoneCallback = typing.Callable[[CommandReplyType, bool, str], None]
CommandFailCallback = typing.Callable[[CommandReply.FailType, str], None]


'''
Utility methods for parsing and packing DNS record data.
'''

import io
import logging
import struct
from typing import Union

logger = logging.getLogger(__package__)

class ParseError(Exception):
    def __init__(self, data, offset, message):
        super().__init__()
        self.data = data
        self.offset = offset
        self.message = message

    def __repr__(self):
        return f'''<ParseError
    message={self.message}
    offset={self.offset}
    data_len={len(self.data)}
    data={self.data}>'''

def load_domain_name(buffer: bytes, offset: int):
    '''Load a domain name from packed data'''
    parts = []
    cursor = None
    data_len = len(buffer)
    visited = set()
    while offset < data_len:
        if offset in visited:
            raise ParseError(buffer, offset, 'Pointer loop detected')
        visited.add(offset)
        length = buffer[offset]
        offset += 1
        if length == 0:
            if cursor is None:
                cursor = offset
            break
        if length >= 0xc0:
            if cursor is None:
                cursor = offset + 1
            offset = (length - 0xc0) * 256 + buffer[offset]
            continue
        parts.append(buffer[offset:offset + length])
        offset += length
    if cursor is None:
        raise ParseError(buffer, offset, 'Bad data')
    data = b'.'.join(parts).decode()
    return cursor, data

def load_string(buffer: bytes, offset: int):
    '''Load a character string from packed data.'''
    length = buffer[offset]
    offset += 1
    data = buffer[offset:offset + length]
    return offset + length, data

def pack_string(string: Union[str, bytes], btype='B') -> bytes:
    '''Pack string into `{length}{data}` format.'''
    if not isinstance(string, bytes):
        string = string.encode()
    length = len(string)
    return struct.pack('%s%ds' % (btype, length), length, string)

def get_bits(num: int, bit_len: int):
    '''Get lower and higher bits breaking at bit_len from num.'''
    high = num >> bit_len
    low = num - (high << bit_len)
    return low, high

def pack_domain_name(name, names, offset=0):
    parts = name.split('.')
    buf = io.BytesIO()
    while parts:
        subname = '.'.join(parts)
        u = names.get(subname)
        if u:
            buf.write(struct.pack('!H', 0xc000 + u))
            break
        else:
            names[subname] = buf.tell() + offset
        buf.write(pack_string(parts.pop(0)))
    else:
        buf.write(b'\0')
    return buf.getvalue()


from KalmanNet_nn import KalmanNetNN
import torch

if torch.cuda.is_available():
    dev = torch.device("cuda:0")
    torch.set_default_tensor_type("torch.cuda.FloatTensor")
else:
    dev = torch.device("cpu")

def NNBuild(SysModel):

    Model = KalmanNetNN()

    Model.InitSystemDynamics(SysModel.f, SysModel.h, SysModel.m, SysModel.n, SysModel.R, infoString = "partialInfo")
    Model.InitSequence(SysModel.m1x_0, SysModel.m2x_0, SysModel.T)

    Model.InitKGainNet(SysModel.prior_Q, SysModel.prior_Sigma, SysModel.prior_S)

    return Model


import pandas as pd
import numpy as np
from sys import exit

def excluded_tps(data):
    '''
    This function is a short-term solution to excluding tps from the
    ranking and volume files. Necessary until the exclude tp from auto-
    assign logic in functional in the appropriate RedShift table/LCAD.
    Simply takes in the dataframe for the assocaiated market, compares the
    list of tp/los combinations, and filters out any existing in the list.

    Input is the df

    Output is the df with the necessary tp/los combinations removed
    '''
    df = data.copy()

    exclusion_df = pd.read_excel('./data/tp_exclusions.xlsx')

    nan_present = exclusion_df[['los_code', 'provider_code']].isna().any()

    if nan_present.any():
        print('NaN values present in the tp exclusion pertinent columns')
        print(nan_present)
        exit()

    exclusion_df['los_code'] = exclusion_df['los_code'].astype(int)
    # Create the tp
    exclusion_df['tp_los_combination'] = exclusion_df[
        ['provider_code', 'los_code']].astype(str).agg('-'.join, axis=1)

    exclusion_list = exclusion_df['tp_los_combination'].values.tolist()

    df['tp_los_combination'] = df[
        ['provider_code', 'los_code']].astype(str).agg('-'.join, axis=1)

    remove_tp_mask = ~df.tp_los_combination.isin(exclusion_list)
    df = df[remove_tp_mask]

    df.drop(columns='tp_los_combination', inplace=True)

    return df


'''
    Testare graf bipartit folosind BFS
    Cuplajul maxim folosindu-ma de bipartitie si de EK
    pentru fiecare arc cu flux nenul xy din N care nu este
    incident în s sau t, muchia xy corespunzătoare din G se
    adaugă la M)
    TREBUIE FACUT CU GRAF REZIDUAL
'''
def BFS():
    global graf, s, t, n, parent
    visited = [False] * (n+2)
    queue = []
    queue.append(s)
    visited[s] = True
    while queue:
        u = queue.pop(0)
        for ind, val in enumerate(graf[u]):
            if visited[ind] == False and val > 0:
                queue.append(ind)
                visited[ind] = True
                parent[ind] = u
    return True if visited[t] else False

def FordFulkerson():
    global graph, s, t, n, parent

    max_flow = 0

    while BFS():
        path_flow = float("Inf")
        cs = t
        while cs != s:
            path_flow = min(path_flow, graf[parent[cs]][cs])
            cs = parent[cs]

        max_flow += path_flow

        v = t
        while v != s:
            u = parent[v]
            graf[u][v] -= path_flow #arc direct
            graf[v][u] += path_flow #arc invers
            v = parent[v]

    return max_flow
def BIPARTIT():
    global n, graf, start, culoare
    culoare[start] = 1 #colorez primul nod (o sa alternez culorile 0 si 1 )

    coada = []
    coada.append(start)

    while len(coada)>0:
        u = coada.pop()
        if graf[u][u] == 1: #daca s-a scos pe sine
            return False
        for v in range(1,n+1):
            if graf[u][v] == 1 and culoare[v] == -1:#nu a fost colorat
                culoare[v] = 1 - culoare[u]
                coada.append(v)
            #muchie de la u la v cu u si v cu aceeasi culoare => nu e bipartit
            elif graf[u][v] == 1 and culoare[v] == culoare[u]:
                return False
    return True

n=4
#desi sunt 4 adaug cate 2 linii si cate 2 coloane in plus ca sa am
#pt s si t
graf= [
#    s  1  2  3  4  t
    [0, 0, 0, 0, 0, 0], #s
    [0, 0, 1, 0, 1, 0], #1
    [0, 1, 0, 1, 0, 0], #2
    [0, 0, 1, 0, 1, 0], #3
    [0, 1, 0, 1, 0, 0], #4
    [0, 0, 0, 0, 0, 0]  #t
        ]
start=1
culoare = [-1] * (n+1)
if BIPARTIT()==True:
    print("Graful este bipartit")
    # despart varfurile in 2 multimi in functie de culori
    for i in range(1, n + 1):
        if culoare[i] == 1:
            # conectez s-> i
            graf[0][i] = 1
        else:
            # conectez i->t
            graf[i][n + 1] = 1

    s = 0  # S
    t = 5  # T
    parent = [-1] * (n + 2)
    print("Max flow: ", FordFulkerson())
    #un graf bipartit nu contine nicun ciclu de lungime impara
    #mai am muchiile de afisat
    '''
    pentru fiecare arc cu flux nenul xy din N care nu este
incident în s sau t, muchia xy corespunzătoare din G se
adaugă la M) => alg cu graf-ul rezidual
fluxul pt fiecare arc se ia din graful rezidual final:
pentru arcul xy de pe arcul yx din graful rezidual

    '''

else:
    print("Graful NU este bipartit")
    # lungime ciclu impar
    viz = [0] * (n + 1)
    afisare=[]
    tata = [0] * (n + 1)
    ok = 0
    nr=None
    def DF(x):
        global ok, graf, tata, viz, afisare, nr
        viz[x] = 1
        if ok == 0:
            for y in range(n):
                if graf[x][y] and viz[y] == 0:
                    tata[y] = x
                    DF(y)
                    if ok == 1:
                        return
                elif y != tata[x]:
                    print("ciclu elementar")
                    v = x
                    while v != y:
                        afisare.append(v)
                        nr+=1
                        v = tata[v]
                    afisare.append(y)
                    afisare.append(x)
                    if nr%2==1:
                        return
                    else:
                        afisare=[]
                        nr=1
    DF(0)
    print("ciclul este: ", afisare )
    print("lungimea lui este: ", nr)
#print("vectorul de culori: ", culoare[1:])

AN 2 SEM 1/Algoritmi Fundamentali/Rezolvari laburi(nu toate)/L5_2.py


# -*- coding: gb18030 -*-
#
# $Id: Buff_23003.py,v 1.3 2008-02-28 08:25:56 kebiao Exp $

"""
持续性效果
"""

import BigWorld
import csconst
import csstatus
import csdefine
from bwdebug import *
from SpellBase import *
from Function import newUID
from Buff_Shield import Buff_Shield

class Buff_23004( Buff_Shield ):
	"""
	example:使角色受到伤害的12%由出战宠物承担，持续60秒。出战宠物死亡或被收回都会让该效果消失。
	"""
	def __init__( self ):
		"""
		构造函数。
		"""
		Buff_Shield.__init__( self )
		self._param = {}

	def init( self, dict ):
		"""
		读取技能配置
		@param dict: 配置数据
		@type  dict: python dict
		"""
		Buff_Shield.init( self, dict )
		self._p1 = int( dict[ "Param1" ] if len( dict[ "Param1" ] ) > 0 else 0 )  / 100.0

	def isDisabled( self, receiver ):
		"""
		virtual method.
		护盾是否失效
		@param receiver: 受术者
		"""
		return self._param.has_key( "disabled" )#由于护盾是被动态创建出来的 因此可以判断自身数据

	def doBegin( self, receiver, buffData ):
		"""
		Virtual method; call only by real entity.
		效果开始的处理。

		@param receiver: 效果要影响的实体
		@type  receiver: BigWorld.Entity
		@param buffData: BUFF
		@type  buffData: BUFF
		@return: None
		"""
		Buff_Shield.doBegin( self, receiver, buffData )
		buffData[ "skill" ] = self.createFromDict( { "param":{} } )
		receiver.appendShield( buffData[ "skill" ] )

	def doReload( self, receiver, buffData ):
		"""
		Virtual method; call only by real entity.
		效果重新加载的处理。

		@param receiver: 效果要影响的实体
		@type  receiver: BigWorld.Entity
		@param buffData: BUFF
		@type  buffData: BUFF
		@return: None
		"""
		Buff_Shield.doReload( self, receiver, buffData )
		buffData[ "skill" ] = self.createFromDict( { "param":{} } )
		receiver.appendShield( buffData[ "skill" ] )

	def doLoop( self, receiver, buffData ):
		"""
		Virtual method; call only by real entity.
		用于buff，表示buff在每一次心跳时应该做什么。

		@param receiver: 效果要影响的实体
		@type  receiver: BigWorld.Entity
		@param buffData: BUFF
		@type  buffData: BUFF
		@return: BOOL；如果允许继续则返回True，否则返回False
		@rtype:  BOOL
		"""
		"""
		if not receiver.pcg_actPet.isActive() or receiver.pcg_actPet.entity == None:
			receiver.removeShield( self.getUID() )
			return False
		"""
		return Buff_Shield.doLoop( self, receiver, buffData )

	def doEnd( self, receiver, buffData ):
		"""
		Virtual method; call only by real entity.
		效果结束的处理。

		@param receiver: 效果要影响的实体
		@type  receiver: BigWorld.Entity
		@param buffData: BUFF
		@type  buffData: BUFF
		"""
		Buff_Shield.doEnd( self, receiver, buffData )
		receiver.removeShield( buffData[ "skill" ].getUID() )

	def doShield( self, receiver, damageType, damage ):
		"""
		virtual method.
		执行护盾自身功能  如：法术形转化伤害为MP
		注意: 此接口不可手动删除该护盾
		@param receiver: 受术者
		@param damageType: 伤害类型
		@param damage : 本次伤害值
		@rtype: 返回被消减后的伤害值
		"""
		actPet = receiver.pcg_getActPet()
		if self.isDisabled( receiver ) or not actPet :		# 角色无效，或者没有出征宠物
			self._param["disabled"] = True
			return damage

		shieldDamage = int( damage * self._p1 )

		# 这里必须判断real 否则可能会被设置2次， 具体看shieldConsume机制
		if actPet.etype == "REAL" :
			actPet.entity.receiveDamage( 0, self.getSourceSkillID(), csdefine.DAMAGE_TYPE_VOID, shieldDamage ) # 因为是护盾吸收的伤害，所以施法者ID为0

		return damage - shieldDamage

	def addToDict( self ):
		"""
		virtual method.
		打包自身需要传输的数据，数据必须是一个dict，具体参数详看SkillTypeImpl；
		此接口默认返回：{"id":self._id, "param":None}，即表示无动态数据。

		@return: 返回一个SKILL类型的字典。SKILL类型详细定义请参照defs/alias.xml文件
		"""
		return { "param" : self._param }

	def createFromDict( self, data ):
		"""
		virtual method.
		根据给定的字典数据创建一个与自身相同id号的技能。详细字典数据格式请参数SkillTypeImpl。
		此函数默认返回实例自身，这样在一些不需要保存动态数据的技能中就能以更高的效率进行数据还原，
		如果哪些技能需要保存动态数据，则只要重载此接口即可。

		@type data: dict
		"""
		obj = Buff_23004()
		obj.__dict__.update( self.__dict__ )
		obj._param = data["param"]
		if not data.has_key( "uid" ) or data[ "uid" ] == 0:
			obj.setUID( newUID() )
		else:
			obj.setUID( data[ "uid" ] )
		return obj

#
# $Log: not supported by cvs2svn $
#


from cmu_graphics import *
from start import *
from game import *
from help import *
from loadBoard import *
from collections import defaultdict

#Extra Stuff:
#1. Hopefully the UI doesn't look terrible; you can see buttons
#   and messages in classes.py
#2. I think the backtracker is decent? idk I tried my best;
#   all the testing can be seen in test.ipynb
#3. added X-Wing hint generation (ctrl f xwing in game.py)

#reload buttons when resize
def onResize(app):
    app.boardLeft = app.width/10
    app.boardTop = app.height/8
    app.boardWidth = app.boardHeight = min(app.width, app.height)*3/4
    Button.buttons = defaultdict(list)
    start_makeButtons(app)
    game_makeButtons(app)
    loadBoard_makeButtons(app)
    help_makeButtons(app)

def onAppStart(app):
    app.difficulty = 'easy'
    app.font = 'monospace'
    app.showHints = app.wrongLabels = True
    app.saveBoard = False
    app.saveBoardPath = "solvedBoards/"
    app.stepsPerSecond = 120

def main():
    runAppWithScreens(initialScreen='start', height=800, width=1000)

main()


# pylint: disable=no-self-use,invalid-name
import itertools
import math

from pytest import approx, raises
import torch

from allennlp.modules import ConditionalRandomField
from allennlp.modules.conditional_random_field import allowed_transitions
from allennlp.common.checks import ConfigurationError
from allennlp.common.testing import AllenNlpTestCase

from mylib.modules.marginal_conditional_random_field import MarginalConditionalRandomField

class TestConditionalRandomField(AllenNlpTestCase):
    def setUp(self):
        super().setUp()
        self.logits = torch.Tensor([
            [[0, 0, .5, .5, .2], [0, 0, .3, .3, .1], [0, 0, .9, 10, 1]],
            [[0, 0, .2, .5, .2], [0, 0, 3, .3, .1], [0, 0, .9, 1, 1]],
        ])

        self.orig_tags = torch.LongTensor([
            [2, 3, 4],
            [3, 2, 2]
        ])

        self.tags = torch.FloatTensor([
            [[0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]],
            [[0, 0, 0, 1, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]]
        ])
        self.tags = torch.clamp(self.tags + 1e-30, max=1)

        self.transitions = torch.Tensor([
            [0.1, 0.2, 0.3, 0.4, 0.5],
            [0.8, 0.3, 0.1, 0.7, 0.9],
            [-0.3, 2.1, -5.6, 3.4, 4.0],
            [0.2, 0.4, 0.6, -0.3, -0.4],
            [1.0, 1.0, 1.0, 1.0, 1.0]
        ])

        self.transitions_from_start = torch.Tensor([0.1, 0.2, 0.3, 0.4, 0.6])
        self.transitions_to_end = torch.Tensor([-0.1, -0.2, 0.3, -0.4, -0.4])

        # Use the CRF Module with fixed transitions to compute the log_likelihood
        self.crf = MarginalConditionalRandomField(5)
        self.crf.transitions = torch.nn.Parameter(self.transitions)
        self.crf.start_transitions = torch.nn.Parameter(self.transitions_from_start)
        self.crf.end_transitions = torch.nn.Parameter(self.transitions_to_end)

    def score(self, logits, tags):
        """
        Computes the likelihood score for the given sequence of tags,
        given the provided logits (and the transition weights in the CRF model)
        """
        # Start with transitions from START and to END
        total = self.transitions_from_start[tags[0]] + self.transitions_to_end[tags[-1]]
        # Add in all the intermediate transitions
        for tag, next_tag in zip(tags, tags[1:]):
            total += self.transitions[tag, next_tag]
        # Add in the logits for the observed tags
        for logit, tag in zip(logits, tags):
            total += logit[tag]
        return total

    def test_forward_works_without_mask(self):
        log_likelihood = self.crf(self.logits, self.tags).item()

        # Now compute the log-likelihood manually
        manual_log_likelihood = 0.0

        # For each instance, manually compute the numerator
        # (which is just the score for the logits and actual tags)
        # and the denominator
        # (which is the log-sum-exp of the scores for the logits across all possible tags)
        for logits_i, tags_i in zip(self.logits, self.orig_tags):
            numerator = self.score(logits_i.detach(), tags_i.detach())
            all_scores = [self.score(logits_i.detach(), tags_j)
                          for tags_j in itertools.product(range(5), repeat=3)]
            denominator = math.log(sum(math.exp(score) for score in all_scores))
            # And include them in the manual calculation.
            manual_log_likelihood += numerator - denominator

        # The manually computed log likelihood should equal the result of crf.forward.
        assert manual_log_likelihood.item() == approx(log_likelihood)

    def test_forward_works_with_mask(self):
        # Use a non-trivial mask
        mask = torch.LongTensor([
            [1, 1, 1],
            [1, 1, 0]
        ])

        log_likelihood = self.crf(self.logits, self.tags, mask).item()

        # Now compute the log-likelihood manually
        manual_log_likelihood = 0.0

        # For each instance, manually compute the numerator
        #   (which is just the score for the logits and actual tags)
        # and the denominator
        #   (which is the log-sum-exp of the scores for the logits across all possible tags)
        for logits_i, tags_i, mask_i in zip(self.logits, self.orig_tags, mask):
            # Find the sequence length for this input and only look at that much of each sequence.
            sequence_length = torch.sum(mask_i.detach())
            logits_i = logits_i.data[:sequence_length]
            tags_i = tags_i.data[:sequence_length]

            numerator = self.score(logits_i, tags_i)
            all_scores = [self.score(logits_i, tags_j)
                          for tags_j in itertools.product(range(5), repeat=sequence_length)]
            denominator = math.log(sum(math.exp(score) for score in all_scores))
            # And include them in the manual calculation.
            manual_log_likelihood += numerator - denominator

        # The manually computed log likelihood should equal the result of crf.forward.
        assert manual_log_likelihood.item() == approx(log_likelihood)

    def get_all_sequences(self, tags):
        import itertools
        newseq = []
        for marginal in tags:
            newmarginal = []
            for i, val in enumerate(marginal):
                if val > 0:
                    newmarginal.append(i)
            newseq.append(newmarginal)

        all_seqs = itertools.product(*newseq)

        return all_seqs

    def test_forward_works_with_mask_and_marginal(self):
        # Use a non-trivial mask
        mask = torch.LongTensor([
            [1, 1, 1],
            [1, 1, 0]
        ])

        self.tags = torch.FloatTensor([
            [[1, 1, 1, 1, 1], [0, 0, 0, 1, 0], [1, 1, 1, 1, 1]],
            [[0, 0, 0, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]]
        ])

        self.get_all_sequences(self.tags[0].long())

        log_likelihood = self.crf(self.logits, torch.clamp(self.tags + 1e-30, max=1), mask).item()

        # Now compute the log-likelihood manually
        manual_log_likelihood = 0.0

        # For each instance, manually compute the numerator
        #   (which is just the score for the logits and actual tags)
        # and the denominator
        #   (which is the log-sum-exp of the scores for the logits across all possible tags)
        for logits_i, tags_i, mask_i in zip(self.logits, self.tags, mask):
            # Find the sequence length for this input and only look at that much of each sequence.
            sequence_length = torch.sum(mask_i.detach())
            logits_i = logits_i.data[:sequence_length]
            tags_i = tags_i.data[:sequence_length]

            all_seqs = self.get_all_sequences(tags_i)

            num_scores = []
            for seq in all_seqs:
                num_scores.append(self.score(logits_i, seq))

            numerator = math.log(sum(math.exp(score) for score in num_scores))

            all_scores = [self.score(logits_i, tags_j)
                          for tags_j in itertools.product(range(5), repeat=sequence_length)]
            denominator = math.log(sum(math.exp(score) for score in all_scores))
            # And include them in the manual calculation.
            manual_log_likelihood += numerator - denominator

        # The manually computed log likelihood should equal the result of crf.forward.
        assert manual_log_likelihood == approx(log_likelihood)


ip_address = input ("Please enter an IP address: ")

octets = ip_address.split(".")
print("-"*80)
print("{:^15}{:^15}{:^15}{:^15}".format("octet1", "octet2", "octet3", "octet4"))
print("-"*80)
print("Dec{:^15}{:^15}{:^15}{:^15}".format(octets[0], octets[1], octets[2], octets[3]))
print("Bin{:^15}{:^15}{:^15}{:^15}".format(bin(int(octets[0])), bin(int(octets[1])), bin(int(octets[2])), bin(int(octets[3]))))
print(
    "hex{:^15}{:^15}{:^15}{:^15}".format
    (hex(int(octets[0])),
     hex(int(octets[1])),
     hex(int(octets[2])),
     hex(int(octets[3]))
     )
)


import pylab as P

import filaments as F

def mmpsi(r_coils=[0.5, 0.5], r_widths=[0.05, 0.05], n_r_elements=[10, 10],
          z_coils=[-0.25, 0.25], z_widths=[0.15, 0.15], n_z_elements=[10, 10],
          i_coils=[1., 1.], r_range=[0., 1], z_range=[-1, 1],
          r_dim=20, z_dim=20, do_b_r=False, do_b_z=False):

    if len(r_range) == 2:
        r_arr, z_arr = P.meshgrid(P.linspace(r_range[0], r_range[1], r_dim),
                                  P.linspace(z_range[0], z_range[1], z_dim))
    else:
        r_arr, z_arr = r_range, z_range

    psi = 0.
    if do_b_r:
        b_r = 0.
    if do_b_z:
        b_z = 0.
    r_out = []
    z_out = []

    for ii, i_coil in enumerate(i_coils):
        I_0 = i_coil / n_r_elements[ii] / n_z_elements[ii]
        for kk in xrange(1, n_r_elements[ii] + 1):
            r = r_coils[ii] + 0.5 * r_widths[ii] * \
                (1. - (2. * kk - 1) / n_r_elements[ii])
            for jj in xrange(1, n_z_elements[ii] + 1):
                z = z_coils[ii] + 0.5 * z_widths[ii] * \
                    (1. - (2. * jj - 1) / n_z_elements[ii])
                r_out.append(r)
                z_out.append(z)
                psi += F.poloidal_flux_filament(r_arr, z_arr, r, z, I_0)
                if do_b_r:
                    b_r += F.b_r_filament(r_arr, z_arr, r, z, I_0)
                if do_b_z:
                    b_z += F.b_z_filament(r_arr, z_arr, r, z, I_0)

    if not do_b_r:
        b_r = 0. * psi
    if not do_b_z:
        b_z = 0. * psi

    return r_arr, z_arr, psi, b_r, b_z, r_out, z_out


import torch
import torch.nn as nn
from torch import optim
from torchvision import datasets, transforms
from torch.utils.data import random_split, DataLoader

train_data = datasets.MNIST('data', train=True, download=True, transform=transforms.ToTensor())
train, val = random_split(train_data, [55000, 5000])
train_loader = DataLoader(train, batch_size=32)
val_loader = DataLoader(val, batch_size=32)

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

        self.features = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=5, kernel_size=3, padding=1),
                                      nn.ReLU(inplace=True), nn.BatchNorm2d(5),
                                      nn.Conv2d(in_channels=5, out_channels=10, kernel_size=3, padding=1),
                                      nn.MaxPool2d(2, 2), nn.ReLU(inplace=True), nn.BatchNorm2d(10),
                                      nn.Conv2d(in_channels=10, out_channels=20, kernel_size=3, padding=1),
                                      nn.ReLU(inplace=True), nn.BatchNorm2d(20),
                                      nn.Conv2d(in_channels=20, out_channels=40, kernel_size=3, padding=1),
                                      nn.MaxPool2d(2, 2), nn.ReLU(inplace=True), nn.BatchNorm2d(40))

        self.classifier = nn.Sequential(nn.Linear(7 * 7 * 40, 1024), nn.ReLU(inplace=True),
                                       	nn.Linear(1024, 2048), nn.ReLU(inplace=True),
                                        nn.Linear(2048, 10))

    def forward(self, x):

        x = self.features(x)
        x = x.view(-1, 7 * 7 * 40)
        x = self.classifier(x)
        return x

# Instantiation of the network
model = Net()

# Instantiation of the cross-entropy loss
loss = nn.CrossEntropyLoss()

# Instantiation of the Adam optimizer
optimizer = optim.Adam(model.parameters(), lr=3e-4, weight_decay=0.001)

nb_epochs = 10
for epoch in range(nb_epochs):
  losses = list()
  accuracies = list()
  model.train()
  for batch in train_loader:
    x, y = batch
    optimizer.zero_grad()

    # Computing the forward pass
    outputs = model(x)

    # Computing the loss function
    J = loss(outputs, y)

    # Computing the gradients
    J.backward()

    # Updating the weights
    optimizer.step()
    losses.append(J.item())
    accuracies.append(y.eq(outputs.detach().argmax(dim=1)).float().mean())
  print(f'Epoch {epoch + 1}', end=', ')
  print(f'training loss: {torch.tensor(losses).mean() : .2f}', end=', ')
  print(f'training accuracy: {torch.tensor(accuracies).mean(): .2f}')

  losses = list()
  accuracies = list()
  model.eval()
  for batch in val_loader:
    x, y = batch

    # Computing the forward pass
    with torch.no_grad():
      outputs = model(x)

    # Computing the loss function
    J = loss(outputs, y)
    losses.append(J.item())
    accuracies.append(y.eq(outputs.detach().argmax(dim=1)).float().mean())
  print(f'Epoch {epoch + 1}', end=', ')
  print(f'validation loss: {torch.tensor(losses).mean() : .2f}', end=', ')
  print(f'validation accuracy: {torch.tensor(accuracies).mean(): .2f}')


import requests
from loguru import logger

from utils.config import config

def is_unlock(name, mix_port, curr_node, total_node):
    try:
        headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                                 "Chrome/64.0.3282.119 Safari/537.36"}
        flag = 0
        if not config['fastMode']:
            # 自制剧
            r1 = requests.get("https://www.netflix.com/title/70242311", proxies={
                "http": f"socks5h://127.0.0.1:{mix_port}",
                "https": f"socks5h://127.0.0.1:{mix_port}"
            }, timeout=5, headers=headers)
            if r1.status_code == 200:
                flag += 1
        else:
            flag += 1

        # 非自制剧
        r2 = requests.get("https://www.netflix.com/title/70143836", proxies={
            "http": f"socks5h://127.0.0.1:{mix_port}",
            "https": f"socks5h://127.0.0.1:{mix_port}"
        }, timeout=5, headers=headers)
        if r2.status_code == 200:
            flag += 1

        # 解锁测试
        if flag == 0:
            logger.info(f"({curr_node}/{total_node}) {name}: None.")
            ntype = "None"
        elif flag == 1:
            if config['fastMode']:
                logger.info(f"({curr_node}/{total_node}) {name}: None.")
                ntype = "None"
            else:
                logger.info(f"({curr_node}/{total_node}) {name}: Only Original.")
                ntype = "Only Original"
        else:
            logger.info(f"({curr_node}/{total_node}) {name}: Full Unlock.")
            ntype = "Full Unlock"

    except Exception as e:
        logger.error(f"({curr_node}/{total_node}) {name}：Error. {str(e.args[0].args[0])}")
        ntype = "Error"
    return ntype


import findspark
spark = findspark.init()

import pyspark
import random
from pyspark.sql import SQLContext, functions as func, types as typ
from pyspark.sql.functions  import explode, split, avg, col, desc, asc, count, countDistinct, substring, rtrim
from pyspark.ml.recommendation import ALS
from pyspark.ml.evaluation import RegressionEvaluator
import matplotlib.pyplot as plt
import SparkPractical1 as funcs1
import HelperFunctions as helperFuncs
import SparkPractical_module2 as module2
sc = pyspark.SparkContext.getOrCreate()
## Read the data into memory

sqlContest = SQLContext(sc)

SMALL_DATASET_PATH = 'ml-latest-small'
DATASET_PATH = 'ml-latest'

def load_data(dataPath):
    #Load the movies and rating CSV files
    movies = sqlContest.read.csv(dataPath + '/movies.csv', header=True)
    ratings = sqlContest.read.csv(dataPath + '/ratings.csv', header=True)
    tags = sqlContest.read.csv(dataPath + '/tags.csv', header=True)

    return ratings, movies, tags

def clean_data(ratings, movies):

    ##Convert the timestamp to date
    # Drop NA values
    # Convert data Type of columns intro integer and float
    clean_ratings = ratings.na.drop()\
    .withColumn('watching_date', func.date_format(ratings.timestamp.
                                                           cast(dataType= typ.LongType()).
                                                           cast(dataType= typ.TimestampType()),
                                                           "yyyy-MM-dd"))\
    .withColumn("rating", ratings.rating.cast(dataType = typ.FloatType()))\
    .withColumn("userId", ratings.userId.cast(dataType = typ.IntegerType()))\
    .withColumn("movieId", ratings.movieId.cast(dataType = typ.IntegerType()))

    # Drop NA values
    # Convert data Type of columns into integer

    clean_movies = movies.na.drop()\
    .withColumn("movieId", movies.movieId.cast(dataType = typ.IntegerType()))\
    .withColumn("production_year", substring(rtrim(col("title")), -5, 4))

    #Split the genres column into rows based on the number of the separator |

    movies_geners =movies.withColumn("Genr",explode(split("genres","[|]")))

    ## return the clean datafrmes (ratings, movies, and movies genres)

    return clean_ratings, clean_movies, movies_geners

def controlPanel():

    #Select the dataset
    data_path = SMALL_DATASET_PATH

    data_select = int(input("Please enter the number (1) to select the small dataset or (2) to use the big dataset:").strip())
    if data_select == 2:
        data_path = DATASET_PATH

    #Load the datasets
    ratings, movies, tags = load_data(data_path)

    #Clean the dataset
    ratings, movies, movies_genres = clean_data(ratings, movies)
    #ratings.show()
    #movies.show()

    # Transform tables
    movies_genres_watched =  helperFuncs.get_movies_genres_watched(ratings, tags, movies)

    #presist the data in memory
    movies.persist()
    ratings.persist()
    movies_genres_watched.persist()

    ## Prepare the recommendation system
    if data_select != 2:
        als_model = module2.train_als_model(ratings)
        print("Recommendations system is ready now....")
    else:
        print("Recommendation can work only with the small data because of the limited resources!")
    #Set the number of records to show
    N = 7

    #Interact with the dataset
    while True:
        print("Choices:")
        print("1- Search user | 2-Search Users | 3-Search Genre | 4-Search Genres")
        print("5- Count movies watched by ID | 6- Count genres watched by ID | 7- Search movies watched by IDs")
        print("8- Summarise movie average rating and watch count by id| 9- Summarise movie average rating and watch count by title")
        print("10- Search movies in genres| 11- List top wathed movies | 12- List most rated Movies | 13- search movies by year")
        print("14- Show user's favourite genres | 15- compare two users' taste | 16- Show visualizations ")
        print("17- Recommend Movies for user | 18- Print Data Sumary | 19- Exit")

        try:
            choice = int(input('Enter your choice number from the list above:').strip())

            if choice == 1: ## Search user by ID (Ashraf)
                user_id = helperFuncs.enterId("user ")
                module2.show_user_details(user_id, movies_genres, ratings, movies)

            elif choice == 2: ## Search many Users by ID (Ashraf)
                users_ids = helperFuncs.enterUsersIds()
                for user_id in users_ids:
                    module2.show_user_details(user_id, movies_genres, ratings, movies)

            elif choice == 3:   # Search for movies of specified genre(Ashraf)
                genre = input("Please enter the genre name:").strip()
                module2.show_genre_details(genre, movies)

            elif choice == 4:   # Search for movies of specified genres (Ashraf)
                genres = helperFuncs.enterGenres()
                for genre in genres:
                    module2.show_genre_details(genre, movies)

            elif choice == 5:   # Count number of movies user has watched (Karl)
                userId = helperFuncs.enterId("user ")
                funcs1.count_watched_movies(movies_genres_watched, userId).show()

            elif choice == 6:   # Count number of genres user has watched (Karl)
                userId = helperFuncs.enterId("user ")
                funcs1.count_watched_genres(movies_genres_watched, userId).show()

            elif choice == 7:   # Get titles of movies watched by users (Karl)
                userIds = helperFuncs.enterUsersIds()
                funcs1.get_titles_of_movies_watched_ids(movies_genres_watched, userIds).show()

            elif choice == 8:   # Get watchcount and avg rating of movie by its id (Karl)
                movieId = helperFuncs.enterId("movie ")
                funcs1.summarise_avg_rating_watchcount_movie(helperFuncs.search_movie_by_id(movieId, movies_genres_watched)).show()

            elif choice == 9:   # Get watchcount and avg rating of movie by its title (Karl)
                title = input("Please enter the movie title:").strip()
                funcs1.summarise_avg_rating_watchcount_movie(helperFuncs.search_movie_by_title(title, movies_genres_watched)).show()

            elif choice == 10:  # Search for movies in given genre (Karl)
                genres = helperFuncs.enterGenres()
                funcs1.search_movies_in_genres(movies_genres_watched, genres).show(100)

            elif choice == 11:  # Show top n rated movies (Ashraf)
                n = int(input("please enter the number of top wacthed movies to show:").strip())
                module2.list_top_watched(ratings, movies).show(n)

            elif choice == 12:  # Search movie by year(Ashraf)
                n = input("Pplease enter the number of top rated movies to show:")
                module2.list_top_rated(ratings, movies).show(n)

            elif choice == 13:  # Search movie by year(Ashraf)
                year = input("Please enter a year between 1750 - 2018:  ")
                module2.search_movies_by_year(year, movies).show()

            elif choice == 14:  # Find favourite genre of user (Karl)
                user_id = helperFuncs.enterId("user ")
                funcs1.search_favourite_genre(user_id, movies_genres_watched).show()

            elif choice == 15:  # Compare user tastes (Ashraf)
                user1_Id = helperFuncs.enterId("First user ")
                user2_Id = helperFuncs.enterId("Second user ")
                summary, u1_diff, u2_diff, u1_and_u2_like, u1_and_u2_dislike, u1_like_u2_dislike, u1_dislike_u2_like = (
                module2.compareTastes(user1_Id, user2_Id, movies_genres, ratings, movies))

                print("Comparing the Tastes of user:" +
                     str(user1_Id) + " , and user:" + str(user2_Id) + " :" )
                print("Both users are watching the following genres, the table below show compare there watching times and ratings:")
                summary.show()
                print("The next Genres are favourite for both user:" +
                     str(user1_Id) + " , and user:" + str(user2_Id) + " :" )
                u1_and_u2_like.show()
                print("The next Genres are unfavourite for both user:" +
                     str(user1_Id) + " , and user:" + str(user2_Id) + " :" )
                u1_and_u2_dislike.show()
                print("The next Genres are preffered by user:" +
                     str(user1_Id) + " , but disliked by user:" + str(user2_Id) + " :" )
                u1_like_u2_dislike.show()
                print("The next Genres are preffered by user:" +
                     str(user1_Id) + " , but disliked by user:" + str(user2_Id) + " :" )
                u1_dislike_u2_like.show()

                print("User " + str(user1_Id) + " watched " + str(u1_diff.count()) + " Genres that User " + str(user2_Id) + " have not watched :")
                u1_diff.show()
                print("User " + str(user2_Id) + " watched " + str(u2_diff.count()) + " Genres that User " + str(user1_Id) + " have not watched :")
                u2_diff.show()

            elif choice == 16:  # Visualise (Karl)
                funcs1.select_visualisations(movies_genres_watched)

            elif choice == 17: ## Recommend movies (Ashraf)
                user_id = module2.enterId("user ")
                print("The top 10 recommended movies for this user are:")
                rec_movies = module2.recommend_movies(als_model, user_id, 10, movies)
                rec_movies.show()
            elif choice == 18: ## Summarize Data (Ashraf)
                module2.summarize_all_data(ratings, movies, movies_genres)

            elif choice == 19:
                break

        except ValueError:
            print("You should enter only a number between 1 and 17!")

controlPanel()


import discord
import codecs
import time
import sys
import traceback
import yaml
import aiohttp

from discord.ext import commands
from discord.ext.commands.view import StringView
from cogs.utils.settingsmanager import Settings
from cogs.utils.localizer import Localizer
from cogs.utils.localizer import LocalizerWrapper
from cogs.utils.alias import Aliaser
from cogs.utils.context import Context
from cogs.utils.logger import BotLogger
from cogs.helpformatter import commandhelper
from cogs.utils.paginator import Scroller

with codecs.open("data/config.yaml", 'r', encoding='utf8') as f:
    conf = yaml.load(f, Loader=yaml.SafeLoader)

initial_extensions = [
    'cogs.cogs',
    'cogs.settings',
    'cogs.misc',
    'cogs.helpformatter'
]

on_ready_extensions = [
    'cogs.music',
    'cogs.musicevents'
]

def _get_prefix(bot, message):
    if not message.guild:
        prefix = bot.settings.default_prefix
        return commands.when_mentioned_or(prefix)(bot, message)
    prefixes = bot.settings.get(message.guild, 'prefixes', 'default_prefix')
    return commands.when_mentioned_or(*prefixes)(bot, message)

class Bot(commands.Bot):
    def __init__(self, debug: bool=False):
        super().__init__(command_prefix=_get_prefix,
                         description=conf["bot"]["description"])

        self.settings = Settings(**conf['default server settings'])
        self.APIkeys = conf.get('APIkeys', {})

        self.localizer = Localizer(conf.get('locale path', "./localization"), conf.get('locale', 'en_en'))
        self.aliaser = Aliaser(conf.get('locale path', "./localization"), conf.get('locale', 'en_en'))

        self.session = aiohttp.ClientSession(loop=self.loop)

        self.debug = debug
        self.main_logger = logger
        self.logger = self.main_logger.bot_logger.getChild("Bot")
        self.logger.debug("Debug: %s" % debug)

        for extension in initial_extensions:
            try:
                self.load_extension(extension)
            except Exception:
                self.logger.exception("Loading of extension %s failed" % extension)

    async def on_command_error(self, ctx, err):
        if not self.debug:
            if (isinstance(err, commands.MissingRequiredArgument) or
                    isinstance(err, commands.BadArgument)):
                paginator = commandhelper(ctx, ctx.command, ctx.invoker, include_subcmd=False)
                scroller = Scroller(ctx, paginator)
                await scroller.start_scrolling()

            if isinstance(err, commands.CommandInvokeError):
                self.logger.debug("Error running command: %s\n Traceback: %s"
                                 % (ctx.command, traceback.format_exception(err.__traceback__)))
                pass

            elif isinstance(err, commands.NoPrivateMessage):
                self.logger.debug("Error running command: %s\n Traceback: %s"
                                 % (ctx.command, traceback.format_exception(err.__traceback__)))
                await ctx.send('That command is not available in DMs')

            elif isinstance(err, commands.CommandOnCooldown):
                await ctx.send(f"{ctx.message.author.mention} Command is on cooldown. Try again in `{err.retry_after:.1f}` seconds.")

            elif isinstance(err, commands.CheckFailure):
                self.logger.debug("Error running command: %s\n Traceback: %s"
                                 % (ctx.command, traceback.format_exception(err.__traceback__)))
                pass

            elif isinstance(err, commands.CommandNotFound):
                pass
        else:
            tb = err.__traceback__
            traceback.print_tb(tb)
            print(err)

    async def on_message(self, message):
        if message.author.bot:
            return
        await self.process_commands(message)

    async def process_commands(self, message):
        ctx = await self.get_context(message, cls=Context)

        # Replace aliases with commands
        ctx = self.aliaser.get_command(ctx)

        # Add the localizer
        if ctx.command and ctx.command.cog_name:
            ctx.localizer = LocalizerWrapper(self.localizer, ctx.locale, ctx.command.cog_name.lower())
        else:
            ctx.localizer = LocalizerWrapper(self.localizer, ctx.locale, None)

        await self.invoke(ctx)

    async def on_ready(self):
        if not hasattr(self, 'uptime'):
            self.uptime = time.time()

        for extension in on_ready_extensions:
            try:
                self.logger.debug("Loading extension %s" % extension)
                self.load_extension(extension)
            except Exception as e:
                self.logger.exception("Loading of extension %s failed" % extension)

        print(f'\nLogged in as: {self.user.name}' +
              f' in {len(self.guilds)} servers.')
        print(f'Version: {discord.__version__}\n')

        await self.change_presence(activity=discord.Game(type=0,
                                   name=conf["bot"]["playing status"]),
                                   status=discord.Status.online)

    def run(self):
        try:
            super().run(conf["bot"]["token"], reconnect=True)
        except Exception as e:
            tb = e.__traceback__
            traceback.print_tb(tb)
            print(e)

def run_bot(debug: bool=False):
    bot = Bot(debug=debug)
    bot.run()

if __name__ == '__main__':
    state = False
    if 'debug' in sys.argv:
        state = True
    logger = BotLogger(state, conf.get('log_path', '/data/logs'))
    run_bot(debug=state)


import splitfolders as sf
import os
import argparse

# Change the working directory to the location of the script
os.chdir(os.path.dirname(os.path.abspath(__file__)))

def split(from_path, to_path, train, val, test, seed):
    """Splits the dataset into train, val, and test where train is 80%, val is 10% and test is 10%

    from_path (str): Path to folder containing unsplit images
    to_path (str): Path to folder where the split images will be stored
    train (float): Percentage of data to trainable data
    val (float): Percentage of data to be validation data
    test (float): Percentage of data to be test data
    seed (float): Seed for the image selection

    """
    sf.ratio(from_path, to_path, seed=seed, ratio=(train,val,test))

def define_arguments(parser):
    """Defines the arguments for the script

    Args:
        parser (argparse.ArgumentParser): The parser to add the arguments to
    """
    parser.add_argument('-s', '--source', type=str, help='Path to folder containing unsplit images')
    parser.add_argument('-d', '--destionation', type=str, help='Path to folder where the split images will be stored')
    parser.add_argument('--train', type=float, default=0.8, help='Training set precentage (default: 0.8)')
    parser.add_argument('--val', type=float, default=0.1, help='Validation set precentage (default: 0.1)')
    parser.add_argument('--test', type=float, default=0.1, help='Test set precentage (default: 0.1)')
    parser.add_argument('--seed', type=float, default=1337, help='Seed for the image selection (default: 1337)')

if __name__ == "__main__":
    parser = argparse.ArgumentParser(prog = 'DATA SET SPLITTER', description='Split dataset into train, val, and test set')
    define_arguments(parser)
    args = parser.parse_args()
    split(args.source, args.destionation, args.train, args.val, args.test, args.seed)


from datetime import date

from django.http import JsonResponse

from workoutentry.modelling.modelling_types import DayAggregation, Aggregation, PandasPeriod, ReadingEnum
from workoutentry.modelling.period import Period
from workoutentry.modelling.processor import TSBProcessor, TimeSeriesProcessor
from workoutentry.modelling.rolling import RollingDefinition, NoOpRoller
from workoutentry.modelling.time_period import TimePeriod
from workoutentry.modelling.time_series import TimeSeriesManager
from workoutentry.modelling.data_definition import DataDefinition, SeriesDefinition
from workoutentry.training_data import TrainingDataManager
from workoutentry.views.json.response import TrainingDiaryResponse
from workoutentry.views.training_diary_resource import TrainingDiaryResource

class BikeSummary(TrainingDiaryResource):

    URL = '/bike/summary/'

    def call_resource(self, request):
        response = TrainingDiaryResponse()
        tdm = TrainingDataManager()
        summary = tdm.bike_summary()
        bikes = list()
        totals = {'name': 'Total'}
        for bike, years in summary.items():
            for year, value in years.items():
                y = totals.get(year, 0)
                y += value
                totals[year] = y
            years['name'] = bike
            bikes.append(years)
        bikes.append(totals)
        response.add_data('bikes', bikes)
        years = list()
        for k, v in summary.items():
            years = [y for y in sorted(v.keys(), reverse=True)]
            break
        response.add_data('years', years)

        return JsonResponse(data=response.as_dict())

class TrainingSummary(TrainingDiaryResource):

    URL = '/training/summary/'

    def call_resource(self, request):
        response = TrainingDiaryResponse()
        tdm = TrainingDataManager()
        summary = tdm.training_annual_summary()
        totals = {"name": 'Total'}
        years = list()
        for yr, v in summary.items():
            for activity, dd in v.items():
                a_dd = totals.get(activity, {'km': 0, 'seconds': 0, 'tss': 0})
                a_dd['km'] += dd['km']
                a_dd['seconds'] += dd['seconds']
                a_dd['tss'] += dd['tss']
                totals[activity] = a_dd
            v['name'] = yr
            years.append(v)
        years.append(totals)
        # response.add_data('summary', summary)
        response.add_data('years', years)
        return JsonResponse(data=response.as_dict())

class ReadingSummary(TrainingDiaryResource):

    URL = '/reading/summary/'

    def call_resource(self, request):

        period_year = Period(pandas_period=PandasPeriod.Y_DEC,
                             aggregation=Aggregation.MEAN,
                             to_date=False,
                             incl_zeroes=False)

        series_definition = SeriesDefinition(period=period_year, rolling_definition=NoOpRoller())

        time_series_sets = list()

        for reading in ReadingEnum:
            dd = DataDefinition(measure=reading.value, day_aggregation_method=DayAggregation.MEAN)
            time_series_sets.append(TimeSeriesManager.TimeSeriesSet(data_definition=dd,
                                                                    series_definition=series_definition,
                                                                    processor=TimeSeriesProcessor.get_processor("No-op")))

        diary_time_period = TrainingDataManager().diary_time_period()
        # year summaries do need time period to be to year end
        diary_time_period.end = date(diary_time_period.end.year, 12, 31)
        tsl, errors = TimeSeriesManager().time_series_list(requested_time_period=diary_time_period, time_series_list=time_series_sets)

        if len(tsl) > 0:
            total_series = {'date': "Total"}
            for k in tsl[0].keys():
                if k != 'date':
                    entries = [d[k] for d in tsl]
                    year_count = sum([1 for e in entries if e > 0])
                    total_series[k] = 0 if year_count == 0 else sum(entries) / year_count
            tsl.append(total_series)

        for dd in tsl:
            dd['name'] = dd['date']

        response = TrainingDiaryResponse()
        [response.add_message(response.MSG_ERROR, e) for e in errors]
        response.add_data('time_series', tsl)

        return JsonResponse(data=response.as_dict())

class CannedGraph(TrainingDiaryResource):

    URL = '/training/data/canned/'

    def required_post_fields(self):
        return ['year', 'activity', 'graph']

    def call_resource(self, request):
        response = TrainingDiaryResponse()
        tms = TimeSeriesManager()
        graph = request.POST['graph']
        activity = request.POST['activity']
        year_str = self._normalise_year_str(request.POST['year'])
        yr_title = year_str if year_str != 'Total' else "All Time"

        if year_str == 'Total':
            tdm = TrainingDataManager()
            tp = tdm.diary_time_period()
        else:
            year = int(year_str)
            tp = TimePeriod(date(year,1,1), date(year,12,31))

        tss_list = list()
        if graph == 'tss':
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=DataDefinition(activity='All' if activity == "Total" else activity,
                                                                                activity_type='All',
                                                                                equipment='All',
                                                                                measure='tss',
                                                                                day_aggregation_method=DayAggregation.SUM),
                                                            processor=TSBProcessor(7, 7, 42, 42)))
        elif graph == 'duration':
            duration_defn = DataDefinition(activity='All' if activity == "Total" else activity,
                                           activity_type='All',
                                           equipment='All',
                                           measure='hours',
                                           day_aggregation_method=DayAggregation.SUM)
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=duration_defn))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=duration_defn, series_definition=SeriesDefinition(Period(), RollingDefinition(7, Aggregation.SUM))))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=duration_defn, series_definition=SeriesDefinition(Period(PandasPeriod.Y_DEC, to_date=True))))
        elif graph == 'km':
            km_defn = DataDefinition(activity='All' if activity == "Total" else activity,
                                     activity_type='All',
                                     equipment='All',
                                     measure='km',
                                     day_aggregation_method=DayAggregation.SUM)
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=km_defn))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=km_defn, series_definition=SeriesDefinition(Period(), RollingDefinition(7, Aggregation.SUM))))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=km_defn, series_definition=SeriesDefinition(Period(PandasPeriod.Y_DEC, to_date=True))))
        elif graph == 'reading':
            reading_defn = DataDefinition(activity='All',
                                          activity_type='All',
                                          equipment='All',
                                          measure=activity,
                                          day_aggregation_method=DayAggregation.MEAN)
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=reading_defn, series_definition=SeriesDefinition(period=Period(incl_zeroes=False))))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=reading_defn, series_definition=SeriesDefinition(Period(), RollingDefinition(7, Aggregation.MEAN))))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=reading_defn, series_definition=SeriesDefinition(Period(), RollingDefinition(31, Aggregation.MEAN))))
        elif graph == 'bike':
            bike_defn = DataDefinition(activity='Bike',
                                       activity_type='All',
                                       equipment='All' if activity == 'Total' else activity,
                                       measure='km',
                                       day_aggregation_method=DayAggregation.SUM)
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=bike_defn))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=bike_defn,
                                                            series_definition=SeriesDefinition(Period(), RollingDefinition(7, Aggregation.SUM))))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=bike_defn,
                                                            series_definition=SeriesDefinition(Period(PandasPeriod.Y_DEC, Aggregation.SUM, to_date=True))))

        if len(tss_list) > 0:
            values = tms.time_series_graph(requested_time_period=tp, time_series_list=tss_list)
        else:
            values = {'title': "No Data"}

        response.add_data('chart_title', f"{yr_title} {values['title']}")
        response.add_data('time_series', values)
        return JsonResponse(data=response.as_dict())

    def _normalise_year_str(self, year_str) -> str:
        if year_str == "Total":
            return year_str
        return year_str[:4]


import json
import os
import pystache

class Parser:

    def __init__(self, input_dir='/tmp/table_data/'):

        self.input_dir = input_dir
        self.template_dir = "{}/templates/".format(os.path.dirname(os.path.realpath(__file__)))
        self.parse_type = "BASE"

        self.person_org_table = self.load_record("person_organization")
        self.org_org_table = self.load_record("organization_organization")
        self.corporate_entity_type_table = self.load_record("corporate_entity_type")
        self.institution_table = self.load_record("institution")
        self.corporate_entity_table = self.load_record("corporate_entity")
        self.geo_table = self.load_record("geography")
        self.township_table = self.load_record("township")
        self.county_table = self.load_record("county")
        self.prefecture_table = self.load_record("prefecture")
        self.province_table = self.load_record("province")
        self.nationality_table = self.load_record("nationality")
        self.religious_family_table = self.load_record("religious_family")

        self.records = None

    def load_record(self, rec_type):
        """
        Load a table from file
        """

        with open("{0}/{1}.json".format(self.input_dir, rec_type), 'r') as f:

            ret = json.loads(f.read())

        return ret

    def map_to_coords(self):
        """
        Overridden in subclasses
        """

        return {}

    def _religious_family_mapping(self, fam_id):

        return self.religious_family_table[fam_id]["name_en"].lower()

    def _corp_type_mapping(self, type_id):

        return self.corporate_entity_type_table[type_id]["type_en"].lower()

    def grab_province_from_township(self, t_rec):

        try:
            county_rec = t_rec["county_id"][0]
            c_rec = self.county_table[county_rec]
            pref_rec = c_rec["prefecture_id"][0]
            p_rec = self.prefecture_table[pref_rec]
            prov_rec = p_rec["province_id"][0]
            province_record = self.province_table[prov_rec]
            try:
                name_en = province_record["name_en"]
            except KeyError:
                name_en = "N/A"
            try:
                name_py = province_record["name_py"]
            except KeyError:
                name_py = "N/A"
            try:
                name_zh = province_record["name_zh"]
            except KeyError:
                name_zh = "N/A"
            return {"name_en": name_en.lower(), "name_py": name_py.lower(), "name_zh": name_zh}
        except KeyError:
            return {"name_en": "N/A", "name_py": "N/A", "name_zh": "N/A"}

    def grab_province_from_county(self, c_rec):

        try:
            pref_rec = c_rec["prefecture_id"][0]
            p_rec = self.prefecture_table[pref_rec]
            prov_rec = p_rec["province_id"][0]
            province_record = self.province_table[prov_rec]
            try:
                name_en = province_record["name_en"]
            except KeyError:
                name_en = "N/A"
            try:
                name_py = province_record["name_py"]
            except KeyError:
                name_py = "N/A"
            try:
                name_zh = province_record["name_zh"]
            except KeyError:
                name_zh = "N/A"
            return {"name_en": name_en.lower(), "name_py": name_py.lower(), "name_zh": name_zh}
        except KeyError:
            return {"name_en": "N/A", "name_py": "N/A", "name_zh": "N/A"}

    def grab_province_from_prefecture(self, p_rec):

        try:
            prov_rec = p_rec["province_id"][0]
            province_record = self.province_table[prov_rec]
            try:
                name_en = province_record["name_en"]
            except KeyError:
                name_en = "N/A"
            try:
                name_py = province_record["name_py"]
            except KeyError:
                name_py = "N/A"
            try:
                name_zh = province_record["name_zh"]
            except KeyError:
                name_zh = "N/A"
            return {"name_en": name_en.lower(), "name_py": name_py.lower(), "name_zh": name_zh}
        except KeyError:
            return {"name_en": "N/A", "name_py": "N/A", "name_zh": "N/A"}

    def fetch_geo(self, g):
        """
        Map location of a record to a township / county / prefecture / province
        in decreasing order of granularity.
        """

        rec = \
            {
                "coords":
                    {
                        "lat": "N/A",
                        "lon": "N/A"
                    },
                "loc":
                    {
                        "location_type": "N/A",
                        "location_name": "N/A",
                        "location_name_zh": "N/A",
                        "province_name": {}
                    }
            }

        try:
            g_rec = self.geo_table[g]["township_id"][0]
            t_rec = self.township_table[g_rec]

            rec["coords"]["lat"] = t_rec["latitude"]
            rec["coords"]["lon"] = t_rec["longitude"]
            rec["loc"]["location_type"] = "township"
            rec["loc"]["location_name"] = t_rec["name_en"].lower()
            rec["loc"]["location_name_zh"] = t_rec["name_zh"]
            rec["loc"]["province_name"] = self.grab_province_from_township(t_rec)

            return rec

        except KeyError:
            pass

        try:
            g_rec = self.geo_table[g]["county_id"][0]
            c_rec = self.county_table[g_rec]

            rec["coords"]["lat"] = c_rec["latitude"]
            rec["coords"]["lon"] = c_rec["longitude"]
            rec["loc"]["location_type"] = "county"
            rec["loc"]["location_name"] = c_rec["name_en"].lower()
            rec["loc"]["location_name_zh"] = c_rec["name_zh"]
            rec["loc"]["province_name"] = self.grab_province_from_county(c_rec)

            return rec

        except KeyError:
            pass

        try:
            g_rec = self.geo_table[g]["prefecture_id"][0]
            p_rec = self.prefecture_table[g_rec]

            rec["coords"]["lat"] = p_rec["latitude"]
            rec["coords"]["lon"] = p_rec["longitude"]
            rec["loc"]["location_type"] = "prefecture"
            rec["loc"]["location_name"] = p_rec["name_en"].lower()
            rec["loc"]["location_name_zh"] = p_rec["name_zh"]
            rec["loc"]["province_name"] = self.grab_province_from_prefecture(p_rec)

            return rec

        except KeyError:
            pass

        try:
            g_rec = self.geo_table[g]["province_id"][0]
            p_rec = self.province_table[g_rec]

            rec["coords"]["lat"] = p_rec["latitude"]
            rec["coords"]["lon"] = p_rec["longitude"]
            rec["loc"]["location_type"] = "province"
            rec["loc"]["location_name"] = p_rec["name_en"].lower()
            rec["loc"]["location_name_zh"] = p_rec["name_zh"]
            try:
                name_en = p_rec["name_en"]
            except KeyError:
                name_en = "N/A"
            try:
                name_py = p_rec["name_py"]
            except KeyError:
                name_py = "N/A"
            try:
                name_zh = p_rec["name_zh"]
            except KeyError:
                name_zh = "N/A"
            rec["loc"]["province_name"] = \
                {
                    "name_en": name_en.lower(),
                    "name_py": name_py.lower(),
                    "name_zh": name_zh
                }

            return rec

        except KeyError:
            pass

        return None

    def build_records(self):
        """
        Maps each record to its set of filter data, then consolidates
        that list into a GeoJson formatted list of points with all
        matching objects for each point stored in a list on that point.
        """

        records = self.map_to_coords()

        ret = {}
        all_coords = set()
        id_num = 0

        for r in records:

            coords = "{0} {1}".format(r["coords"]["lon"], r["coords"]["lat"])

            if coords in all_coords:
                # merge this record with an existing Point

                del r["coords"]

                ret[coords]["properties"]["objects"].append(r)

            else:
                # create a new Point

                all_coords.add(coords)

                new_coords = [r["coords"]["lon"], r["coords"]["lat"]]
                del r["coords"]

                new_dict = \
                    {
                        "type": "Feature",
                        "id": str(id_num),
                        "geometry":
                            {
                                "type": "Point",
                                "coordinates": new_coords
                            },
                        "properties":
                            {
                                "objects":
                                    [
                                        r
                                    ]
                            }
                    }

                ret[coords] = new_dict
                id_num += 1

        self.records = list(ret.values())

        return self

    def write_records(self, out_path=None):
        """
        Write GeoJson formatted records to file
        """

        if self.records is None:
            self.build_records()

        if out_path is None:
            out_path = "/tmp/geo_{}.js".format(self.parse_type)

        template = open("{}/geo.tmpl".format(self.template_dir)).read()

        data = \
            {
                "DATA": json.dumps(self.records, indent=4, sort_keys=False)
            }

        with open(out_path, 'w', encoding='utf8') as f:
            f.write(pystache.render(template, data))


# 推特允许用户发推特的时候连带地理坐标，我们将使用这些信息绘制地理信息图

# GeoJSON是一个储存地理信息的数据结构，这个数据结构可以储存多种信息并用来绘制地图
# 我们仅需要一个point含有经纬度信息

# 在GeoJSON中我们可以把一个对象视作一个Feature或者一个FeatureCollection
# Feature is basically a geometry with additional properties
# FeatureCollection is a list of features

# 推特数据组可以视作一个FeatureCollection，每一条推特可以是做一个连带地理信息的Feature

# 以下是一个JSON格式范本

"""
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [some_latitude, some_longitude]
            },
            "properties": {
                "text": "This is sample a tweet",
                "created_at": "Sat Mar 21 12:30:00 +0000 2015"
            }
        },
        /* more tweets ... */
    ]
}
"""
# 假设所有数据储存在一个文件中，我们只需要iterate所有推特查找 "coordinates" 一栏

# 所有推特数据储存在 "fname"
with open(fname, 'r') as f:
    geo_data = {
        "type": "FeatureCollection",
        "features": []
    }
    for line in f:
        tweet = json.loads(line)
        if tweet['coordinates']:
            geo_json_feature = {
                "type": "Feature",
                "geometry": tweet['coordinates'],
                "properties": {
                    "text": tweet['text'],
                    "created_at": tweet['created_at']
                }
            }
            geo_data['features'].append(geo_json_feature)

# Save geo data
with open('geo_data.json', 'w') as fout:
    fout.write(json.dumps(geo_data, indent=4))

# 如此这般地理位置信息已经取得

# Javascript可以用来实现python的可视化，尤其是在浏览器方面

Practice 1 with Twitter API/Geolocation and Interactive Maps with GeoJSON and Leaflet.py


#!/usr/bin/env python3

from configparser import ConfigParser

from requests import get

# Set up and performa the request

config = ConfigParser()
config.read('secrets.ini')

base_url = 'https://api.typeform.com/v1/form/' + config['TypeForm']['UID']
args = {'completed': 'true',
        'key': config['TypeForm']['key'],
        }

result = get(base_url, args)

with open('responses/0.json', 'w') as outfile:
    outfile.write(result.text)

# data = result.json()

# submit_dates = [d['metadata']['date_submit'] for d in data['responses']]


from pathlib import Path

# укажите путь к каталогу с файлами
path_dir = Path()

path_file1 = path_dir / 'file1_1.txt'
path_file2 = path_dir / 'file1_2.txt'

with path_file1.open() as fh_in:
    print(f"{type(fh_in)}")
    print(f"{fh_in.read()}")

with path_file2.open(encoding='866') as fh_in:
    text = fh_in.read()

print(text)

text.split()


import sys
from collections import defaultdict, deque

class NumberMonkey:
    def __init__(self, value, name):
        self.value = value
        self.name = name

class OperationMonkey:
    def __init__(self, name, operation, operand1, operand2, op_name):
        self.name = name
        self.operation = operation
        self.operand1 = operand1
        self.operand2 = operand2
        self.op_name = op_name

def step1(graph, monkeys):
    results = solve(graph, monkeys)
    return results['root']

def step2(graph, monkeys):
    humn_monkey = monkeys['humn']
    root_monkey = monkeys['root']
    constants = set(monkeys.keys())
    for i in range(0, 100):
        # find which monkeys that never change value regardless of
        # what value humn_monkey has
        humn_monkey.value = i
        r1 = solve(graph, monkeys)
        humn_monkey.value = i + 1
        r2 = solve(graph, monkeys)
        for m in monkeys.keys():
            if r1[m] != r2[m] and m in constants:
                constants.remove(m)

    def helper(monkey, target):
        """
        Go backwards from root monkey until we come to humn_monkey
        """
        if monkey == humn_monkey:
            return target
        if monkey.operand1 in constants and monkey.operand2 in constants:
            raise ValueError('Unsolveable both sides are contant')
        if monkey.operand1 in constants:
            if monkey == root_monkey:
                return helper(monkeys[monkey.operand2], r1[monkey.operand1])
            elif monkey.op_name == '-':
                return helper(monkeys[monkey.operand2], r1[monkey.operand1] - target)
            elif monkey.op_name == '+':
                return helper(monkeys[monkey.operand2], target - r1[monkey.operand1])
            elif monkey.op_name == '*':
                return helper(monkeys[monkey.operand2], target // r1[monkey.operand1])
            else:
                assert monkey.op_name == '/'
                return helper(monkeys[monkey.operand2], r1[monkey.operand1] // target)
        else:
            assert monkey.operand2 in constants
            if monkey == root_monkey:
                return helper(monkeys[monkey.operand1], r1[monkey.operand2])
            elif monkey.op_name == '-':
                return helper(monkeys[monkey.operand1], target + r1[monkey.operand2])
            elif monkey.op_name == '+':
                return helper(monkeys[monkey.operand1], target - r1[monkey.operand2])
            elif monkey.op_name == '*':
                return helper(monkeys[monkey.operand1], target // r1[monkey.operand2])
            else:
                assert monkey.op_name == '/'
                return helper(monkeys[monkey.operand1], r1[monkey.operand2] * target)

    return helper(root_monkey, None)

def solve(graph, monkeys):
    q = deque()
    for name, monkey in monkeys.items():
        if isinstance(monkey, NumberMonkey):
            q.append(monkey)
    results = {}
    done = set()
    while q:
        monkey = q.popleft()
        done.add(monkey.name)
        if isinstance(monkey, NumberMonkey):
            results[monkey.name] = monkey.value
        else:
            results[monkey.name] = monkey.operation(results[monkey.operand1], results[monkey.operand2])
        for neighbour_name in graph[monkey.name]:
            neighbour = monkeys[neighbour_name]
            if neighbour.operand1 in done and neighbour.operand2 in done:
                q.append(neighbour)
    return results

def read_input():
    graph = defaultdict(list)
    monkeys = {}
    for line in map(str.strip, sys.stdin.readlines()):
        [name, operation] = line.split(':')
        try:
            monkeys[name] = NumberMonkey(int(operation), name)
        except ValueError:
            operations = [('+', lambda x, y: x + y),
                          ('-', lambda x, y: x - y),
                          ('*', lambda x, y: x * y),
                          ('/', lambda x, y: x // y)]
            for c, op in operations:
                if c in operation:
                    [operand1, operand2] = operation.strip().split(' {} '.format(c))
                    monkeys[name] = OperationMonkey(name, op, operand1, operand2, c)
                    graph[operand1].append(name)
                    graph[operand2].append(name)
                    break
    return monkeys, graph

monkeys, graph = read_input()
print(step1(graph, monkeys))
print(step2(graph, monkeys))


from datetime import datetime
from django import template
from django.shortcuts import render, redirect
from django.contrib import messages
from django.shortcuts import get_object_or_404
from account.models import User, UserProfile
from .models import JobPost
from .forms import JobPostForm, CompanyProfileForm
from account.states import STATUS_CHOICES, JOB_STATUS

def employer_profile(request, id=0):
    user = User.objects.get(pk=id)

    if request.method == 'GET':
        if id == user:
            form = CompanyProfileForm()
        else:
            user = UserProfile.objects.get(user_id=id)
            form = CompanyProfileForm(instance=user)
        return render(request, 'employers/company_profile_form.html', {'form': form})
    else:
        if id == user:
            form = CompanyProfileForm(request.POST, request.FILES)
        else:
            user = UserProfile.objects.get(user_id=id)
            form = CompanyProfileForm(request.POST, request.FILES, instance=user or None)
        if form.is_valid():
            profile = form.save(commit=False)
            profile.user = request.user
            profile.save()
        messages.success(request, 'Profile Upadated Successfully !')
        return redirect('employer-dashboard', request.user.pk)

def employer_dashboard(request, id):
    user    = get_object_or_404(User, pk=id)
    posts   = JobPost.objects.all().filter(user=user).order_by('-date_posted')
    context = {
        'user': user,
        'posts': posts,
    }

    return render(request, 'employers/dashboard.html', context)

def post(request):
    if request.method == 'POST':
        form = JobPostForm(request.POST)
        if form.is_valid():
            post_form = form.save(commit=False)
            post_form.user = request.user
            post_form.save()
            messages.success(request, 'Job Listed Successfuly')
            return redirect('employer-dashboard', request.user.pk)
        else:
            context = {
                'form':form
            }
            return render(request, 'employers/post.html', context)
    else:
        context = {
                'form':JobPostForm()
            }
        return render(request, 'employers/post.html', context)

def edit_post(request, id):
    job = get_object_or_404(JobPost, pk=id)
    if request.method == 'POST':
        form = JobPostForm(request.POST, instance=job)
        if form.is_valid():
            edited_form = form.save(commit=False)
            edited_form.user = request.user
            edited_form.save()
            messages.success(request, 'Job Updated Successfuly')
            return redirect('employer-dashboard', request.user.pk)
        else:
            context = {
                'form':form
            }
            return render(request, 'employers/post.html', context)
    else:
        context = {
                'form':JobPostForm(instance=job)
            }
        return render(request, 'employers/post.html', context)

def delete_job(request, id):
    jobpost = JobPost.objects.all().get(pk=id)
    jobpost.delete()
    messages.success(request, 'Job successfully deleted')
    return redirect('employer-dashboard', jobpost.user_id)


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

#Дано натуральное число . Получить все его натуральные делители.

x = int(input("Chislo"))
a = int(1)
while a<=x:
    if x%a==0:
        print("{0}".format(a))
        a = int(a+1)
    else:
        a = int(a+1)


"""
+
NAME:
  mmrs_abtov2v3

PURPOSE:
  Convert MRS local alpha,beta coordinates to JWST v2,v3 coordinates

CALLING SEQUENCE:
  mmrs_abtov2v3,a,b,band,[refdir=]

INPUTS:
  a       - Alpha coordinate in arcsec
  b       - Beta coordinate in arcsec
  band    - band name (e.g, '1A')

OPTIONAL INPUTS:
  refdir - Root directory for distortion files

OUTPUT:
  v2      - V2 coordinate in arcsec
  v3      - V3 coordinate in arcsec

OPTIONAL OUTPUT:
  XAN     - XAN coordinate in arcsec
  YAN     - YAN coordinate in arcsec

COMMENTS:
  Works with CDP5 delivery files.  Inverse function is mmrs_v2v3toab.pro

EXAMPLES:

BUGS:

PROCEDURES CALLED:

INTERNAL SUPPORT ROUTINES:

REVISION HISTORY:
  30-Jul-2015  Written by David Law (dlaw@stsci.edu)
  27-Oct-2015  Add conversion to REAL V2,V3 (D. Law)
  16-Nov-2015  Add conversion to XAN,YAN (D. Law)
  24-Jan-2016  Update reference files to CDP5 (D. Law)
  17-Oct-2016  Input/output v2/v3 in arcsec (D. Law)
  13-Dec-2017  Update directory path for new STScI-MIRI workspace
  10-Oct-2018  Update directory path for new miricoord structure
-
------------------------------------------------------------------------------"""

import numpy as np
from astropy.io import fits

def mmrs_abtov2v3(a,b,band,cdpDir=None,fileversion = "06.04.00"):

    if cdpDir is None:
        cdpDir='/Users/ioannisa/Desktop/python/miri_devel/cdp_data/'

    # Ensure we're not using integer inputs
    adbl=a.astype('float')
    bdbl=b.astype('float')

    # Determine input reference FITS file
    distcdp = {}
    distcdp["3C"] = "MIRI_FM_MIRIFULONG_34LONG_DISTORTION_%s.fits" %fileversion
    distcdp["3B"] = "MIRI_FM_MIRIFULONG_34MEDIUM_DISTORTION_%s.fits" %fileversion
    distcdp["3A"] = "MIRI_FM_MIRIFULONG_34SHORT_DISTORTION_%s.fits" %fileversion

    distcdp["1C"] = "MIRI_FM_MIRIFUSHORT_12LONG_DISTORTION_%s.fits" %fileversion
    distcdp["1B"] = "MIRI_FM_MIRIFUSHORT_12MEDIUM_DISTORTION_%s.fits" %fileversion
    distcdp["1A"] = "MIRI_FM_MIRIFUSHORT_12SHORT_DISTORTION_%s.fits" %fileversion

    distcdp["4C"] = distcdp["3C"]
    distcdp["4B"] = distcdp["3B"]
    distcdp["4A"] = distcdp["3A"]

    distcdp["2C"] = distcdp["1C"]
    distcdp["2B"] = distcdp["1B"]
    distcdp["2A"] = distcdp["1A"]

    # Read alpha,beta -> XAN,YAN table
    convtable=fits.open(cdpDir+distcdp[band])['albe_to_XANYAN'].data
    # Determine which rows we need
    v2index=np.where(convtable.field(0)=='T_CH{}_XAN'.format(band))[0][0]
    v3index=np.where(convtable.field(0)=='T_CH{}_YAN'.format(band))[0][0]
    # Trim to relevant v2, v3 rows for this band
    conv_v2=list(convtable[v2index])
    conv_v3=list(convtable[v3index])

    # WARNING index 0 is just text, the coefficients start from index 1
    xan=conv_v2[1]+conv_v2[2]*adbl + conv_v2[3]*bdbl + conv_v2[4]*adbl*bdbl
    yan=conv_v3[1]+conv_v3[2]*adbl + conv_v3[3]*bdbl + conv_v3[4]*adbl*bdbl

    # convert to arcsec from arcmin
    v2=xan*60.
    v3=-(yan+7.8)*60.
    xan=xan*60.
    yan=yan*60.

    return v2,v3,xan,yan